summaryrefslogtreecommitdiff
path: root/src/client.cc
blob: 6dbd7f3f7d8fd06b1ebe398daecc9fbe63528a2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
// Use of this source code is governed by a apache 2.0 license that can be
// found in the LICENSE file.

#include <aul_svc.h>
#include <bundle.h>
#include <bundle_internal.h>
#include <glib.h>

#include <cstring>
#include <map>
#include <memory>
#include <string>
#include <vector>

#include "include/capability_manager.h"
#include "src/dbus.h"
#include "src/sql_connection.h"
#include "src/sql_statement.h"
#include "src/sqlite_connection.h"
#include "src/utils/logging.h"

#define API __attribute__((visibility("default")))

namespace {

const char kDBPath[] = "/run/capmgr/capmgr.db";

}  // namespace

struct capmgr_device_s {
  std::string device_id;
  std::string model_name;
  std::string device_name;
  std::string platform_ver;
  std::string profile;
  std::string sw_ver;
};

struct capmgr_app_control_s {
  capmgr_device_h device;
  bundle* b;
};

struct capmgr_app_info_s {
  std::string appid;
  std::string pkgid;
  std::string label;
  std::string version;
  capmgr_device_h device;
};

API int capmgr_device_foreach_device(capmgr_device_foreach_cb cb,
    void* user_data) {
  if (!cb)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  std::unique_ptr<capmgr::SQLConnection> sql_conn(
      new capmgr::SQLiteConnection(kDBPath, true));

  const char kQueryForeachDevices[] =
      "SELECT device_id, model_name, device_name, platform_ver,"
      "  profile, sw_ver "
      "FROM devices";
  std::shared_ptr<capmgr::SQLStatement> stmt = sql_conn->PrepareStatement(
      kQueryForeachDevices);
  if (!stmt)
    return CAPMGR_ERROR_IO_ERROR;

  while (stmt->Step() == capmgr::SQLStatement::StepResult::ROW) {
    struct capmgr_device_s dev;
    int idx = 0;
    dev.device_id = stmt->GetColumnString(idx++);
    dev.model_name = stmt->GetColumnString(idx++);
    dev.device_name = stmt->GetColumnString(idx++);
    dev.platform_ver = stmt->GetColumnString(idx++);
    dev.profile = stmt->GetColumnString(idx++);
    dev.sw_ver = stmt->GetColumnString(idx++);
    if (cb(&dev, user_data))
      break;
  }

  return CAPMGR_ERROR_NONE;
}

API int capmgr_device_clone(const capmgr_device_h device,
    capmgr_device_h* device_clone) {
  if (!device || !device_clone)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  try {
    struct capmgr_device_s* clone = new struct capmgr_device_s();

    clone->device_id = device->device_id;
    clone->model_name = device->model_name;
    clone->device_name = device->device_name;
    clone->platform_ver = device->platform_ver;
    clone->profile = device->profile;
    clone->sw_ver = device->sw_ver;

    *device_clone = clone;
  } catch (const std::bad_alloc& e) {
    LOG(ERROR) << e.what();
    return CAPMGR_ERROR_OUT_OF_MEMORY;
  }

  return CAPMGR_ERROR_NONE;
}

API int capmgr_device_destroy(capmgr_device_h device) {
  if (!device)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  delete device;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_device_get_device_id(capmgr_device_h device,
    char** device_id) {
  if (!device || !device_id)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  *device_id = strdup(device->device_id.c_str());
  if (*device_id == nullptr)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_device_get_model_name(capmgr_device_h device,
    char** model_name) {
  if (!device || !model_name)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  *model_name = strdup(device->model_name.c_str());
  if (*model_name == nullptr)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_device_get_device_name(capmgr_device_h device,
    char** device_name) {
  if (!device || !device_name)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  *device_name = strdup(device->device_name.c_str());
  if (*device_name == nullptr)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_device_get_platform_version(capmgr_device_h device,
    char** platform_ver) {
  if (!device || !platform_ver)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  *platform_ver = strdup(device->platform_ver.c_str());
  if (*platform_ver == nullptr)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_device_get_profile(capmgr_device_h device, char** profile) {
  if (!device || !profile)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  *profile = strdup(device->profile.c_str());
  if (*profile == nullptr)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_device_get_sw_version(capmgr_device_h device,
    char** sw_ver) {
  if (!device || !sw_ver)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  *sw_ver = strdup(device->sw_ver.c_str());
  if (*sw_ver == nullptr)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_control_create(capmgr_app_control_h* app_control) {
  if (!app_control)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  try {
    std::unique_ptr<capmgr_app_control_s> control(new capmgr_app_control_s());
    control->b = bundle_create();
    if (!control->b) {
      return CAPMGR_ERROR_OUT_OF_MEMORY;
    }
    int r = aul_svc_set_operation(control->b, AUL_SVC_OPERATION_DEFAULT);
    if (r != AUL_SVC_RET_OK) {
      bundle_free(control->b);
      return CAPMGR_ERROR_OUT_OF_MEMORY;
    }
    *app_control = control.release();
  } catch (const std::bad_alloc& e) {
    LOG(ERROR) << e.what();
    return CAPMGR_ERROR_OUT_OF_MEMORY;
  }

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_control_clone(const capmgr_app_control_h app_control,
    capmgr_app_control_h* app_control_clone) {
  if (!app_control || !app_control_clone)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  try {
    struct capmgr_app_control_s* clone = new struct capmgr_app_control_s();

    int ret = capmgr_device_clone(app_control->device, &clone->device);
    if (ret != CAPMGR_ERROR_NONE) {
      delete clone;
      return CAPMGR_ERROR_OUT_OF_MEMORY;
    }

    clone->b = bundle_dup(app_control->b);
    if (!clone->b) {
      capmgr_app_control_destroy(clone);
      return CAPMGR_ERROR_OUT_OF_MEMORY;
    }

    *app_control_clone = clone;
  } catch (const std::bad_alloc& e) {
    LOG(ERROR) << e.what();
    return CAPMGR_ERROR_OUT_OF_MEMORY;
  }

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_control_destroy(capmgr_app_control_h app_control) {
  if (!app_control)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  capmgr_device_destroy(app_control->device);
  bundle_free(app_control->b);
  delete app_control;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_control_get_device(capmgr_app_control_h app_control,
    capmgr_device_h* device) {
  if (!app_control || !device)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  int ret = capmgr_device_clone(app_control->device, device);
  if (ret != CAPMGR_ERROR_NONE)
    return ret;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_control_get_operation(capmgr_app_control_h app_control,
    char** operation) {
  if (!app_control || !operation)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  const char* val = aul_svc_get_operation(app_control->b);
  if (!val)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  *operation = strdup(val);
  if (*operation == nullptr)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_control_get_uri(capmgr_app_control_h app_control,
    char** uri) {
  if (!app_control || !uri)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  const char* val = aul_svc_get_uri(app_control->b);
  if (!val)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  *uri = strdup(val);
  if (*uri == nullptr)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_control_get_mime(capmgr_app_control_h app_control,
    char** mime) {
  if (!app_control || !mime)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  const char* val = aul_svc_get_mime(app_control->b);
  if (!val)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  *mime = strdup(val);
  if (*mime == nullptr)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_control_get_appid(capmgr_app_control_h app_control,
    char** appid) {
  if (!app_control || !appid)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  const char* val = aul_svc_get_appid(app_control->b);
  if (!val)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  *appid = strdup(val);
  if (*appid == nullptr)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_control_get_extra_data(capmgr_app_control_h app_control,
    const char* key, char** value) {
  if (!app_control || !key || !value)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  // TODO(jeremy.jang): handle reserved key

  const char* val = aul_svc_get_data(app_control->b, key);
  if (!val) {
    LOG(ERROR) << "There is no extra data of key(" << key << ")";
    return CAPMGR_ERROR_INVALID_PARAMETER;
  }

  *value = strdup(val);
  if (*value == nullptr)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_control_set_device(capmgr_app_control_h app_control,
    const capmgr_device_h device) {
  if (!app_control || !device)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  if (app_control->device)
    capmgr_device_destroy(app_control->device);

  int ret = capmgr_device_clone(device, &app_control->device);
  if (ret != CAPMGR_ERROR_NONE)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_control_set_operation(capmgr_app_control_h app_control,
    const char* operation) {
  if (!app_control || !operation)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  int r = aul_svc_set_operation(app_control->b, operation);
  if (r != AUL_SVC_RET_OK)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_control_set_uri(capmgr_app_control_h app_control,
    const char* uri) {
  if (!app_control || !uri)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  int r = aul_svc_set_uri(app_control->b, uri);
  if (r != AUL_SVC_RET_OK)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_control_set_mime(capmgr_app_control_h app_control,
    const char* mime) {
  if (!app_control || !mime)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  int r = aul_svc_set_mime(app_control->b, mime);
  if (r != AUL_SVC_RET_OK)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_control_set_appid(capmgr_app_control_h app_control,
    const char* appid) {
  if (!app_control || !appid)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  int r = aul_svc_set_appid(app_control->b, appid);
  if (r != AUL_SVC_RET_OK)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_control_add_extra_data(capmgr_app_control_h app_control,
    const char* key, const char* value) {
  if (!app_control || !key || !value)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  // TODO(jeremy.jang): handle reserved key

  int r = aul_svc_add_data(app_control->b, key, value);
  if (r != AUL_SVC_RET_OK)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_control_remove_extra_data(capmgr_app_control_h app_control,
    const char* key) {
  if (!app_control || !key)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  // TODO(jeremy.jang): handle reserved key

  int r = bundle_del(app_control->b, key);
  if (r != BUNDLE_ERROR_NONE)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  return CAPMGR_ERROR_NONE;
}

struct cbdata {
  capmgr_app_control_h request;
  capmgr_app_control_reply_cb cb;
  void* user_data;
};

void capmgr_dbus_callback(GVariant* result, void* user_data) {
  LOG(DEBUG) << "Dbus callback for client called";

  // TODO(jeremy.jang): some data maybe returned
  GVariantIter* iter;
  guchar* data;
  guint len;
  g_variant_get(result, "(ayu)", &iter, &len);
  if (!iter)
    LOG(ERROR) << "Some error occurred";

  data = reinterpret_cast<guchar*>(g_try_malloc(len));
  if (!data) {
    LOG(ERROR) << "Out of memory";
    return;
  }

  for (unsigned int i = 0; i < len; i++) {
    if (!g_variant_iter_loop(iter, "y", &data[i])) {
      LOG(ERROR) << "Failed to get data from GVariant!";
      break;
    }
  }
  g_variant_iter_free(iter);

  struct cbdata* cbdata = reinterpret_cast<struct cbdata*>(user_data);
  struct capmgr_app_control_s* reply = new struct capmgr_app_control_s();
  reply->b = bundle_decode(data, len);
  if (!reply->b) {
    LOG(ERROR) << "Invalid bundle data!";
    capmgr_app_control_destroy(cbdata->request);
    delete cbdata;
    return;
  }
  reply->device = cbdata->request->device;

  // TODO(jeremy.jang): need to receive aul_svc result code
  cbdata->cb(cbdata->request, reply, CAPMGR_APP_CONTROL_RESULT_OK,
      cbdata->user_data);

  capmgr_app_control_destroy(cbdata->request);
  delete cbdata;
}

API int capmgr_app_control_send(capmgr_app_control_h app_control,
    capmgr_app_control_reply_cb cb, void* user_data) {
  if (!app_control || !app_control->device || !cb)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  bundle_raw* raw;
  int len;
  int r = bundle_encode(app_control->b, &raw, &len);
  if (r != BUNDLE_ERROR_NONE) {
    LOG(ERROR) << "Failed to encode bundle: " << r;
    return CAPMGR_ERROR_INVALID_PARAMETER;
  }

  // send app_control
  GVariantBuilder* array_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
  for (int i = 0; i < len; i++)
    g_variant_builder_add(array_builder, "y", raw[i]);
  GVariant* gv = g_variant_new("(sayu)",
      app_control->device->device_id.c_str(), array_builder, len);
  g_variant_builder_unref(array_builder);
  bundle_free_encoded_rawdata(&raw);
  if (!gv) {
    LOG(ERROR) << "Failed to create GVariant";
    return CAPMGR_ERROR_INVALID_PARAMETER;
  }
  g_variant_ref_sink(gv);

  capmgr_app_control_h clone;
  r = capmgr_app_control_clone(app_control, &clone);
  if (r != CAPMGR_ERROR_NONE) {
    LOG(ERROR) << "Failed to clone app control request: " << r;
    return r;
  }

  struct cbdata* cbdata = new struct cbdata();
  cbdata->request = clone;
  cbdata->cb = cb;
  cbdata->user_data = user_data;

  if (!capmgr::ProxyCallAsync("SendRemoteAppControl", gv, capmgr_dbus_callback,
        cbdata)) {
    LOG(ERROR) << "Failed to dbus method call";
    delete cbdata;
    capmgr_app_control_destroy(clone);
    g_variant_unref(gv);
    // errcode?
    return CAPMGR_ERROR_INVALID_PARAMETER;
  }

  g_variant_unref(gv);

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_info_clone(
    const capmgr_app_info_h remote_app_info,
    capmgr_app_info_h* remote_app_info_clone) {
  if (!remote_app_info || !remote_app_info_clone)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  struct capmgr_app_info_s* clone;
  try {
    clone = new struct capmgr_app_info_s();
  } catch (const std::bad_alloc& e) {
    LOG(ERROR) << e.what();
    return CAPMGR_ERROR_OUT_OF_MEMORY;
  }

  clone->appid = remote_app_info->appid;
  clone->pkgid = remote_app_info->pkgid;
  clone->label = remote_app_info->label;
  clone->version = remote_app_info->version;

  int ret = capmgr_device_clone(remote_app_info->device, &(clone->device));
  if (ret != CAPMGR_ERROR_NONE) {
    LOG(ERROR) << "Failed to clone capmgr device";
    delete clone;
    return ret;
  }

  *remote_app_info_clone = clone;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_info_destroy(
    capmgr_app_info_h remote_app_info) {
  if (!remote_app_info)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  capmgr_device_destroy(remote_app_info->device);
  delete remote_app_info;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_info_get_appid(
    capmgr_app_info_h remote_app_info, char** appid) {
  if (!remote_app_info || !appid || remote_app_info->appid.empty())
    return CAPMGR_ERROR_INVALID_PARAMETER;

  *appid = strdup(remote_app_info->appid.c_str());
  if (*appid == nullptr)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_info_get_pkgid(
    capmgr_app_info_h remote_app_info, char** pkgid) {
  if (!remote_app_info || !pkgid || remote_app_info->pkgid.empty())
    return CAPMGR_ERROR_INVALID_PARAMETER;

  *pkgid = strdup(remote_app_info->pkgid.c_str());
  if (*pkgid == nullptr)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_info_get_label(
    capmgr_app_info_h remote_app_info, char** label) {
  if (!remote_app_info || !label || remote_app_info->label.empty())
    return CAPMGR_ERROR_INVALID_PARAMETER;

  *label = strdup(remote_app_info->label.c_str());
  if (*label == nullptr)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_info_get_version(
    capmgr_app_info_h remote_app_info, char** version) {
  if (!remote_app_info || !version || remote_app_info->version.empty())
    return CAPMGR_ERROR_INVALID_PARAMETER;

  *version = strdup(remote_app_info->version.c_str());
  if (*version == nullptr)
    return CAPMGR_ERROR_OUT_OF_MEMORY;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_info_get_device(
    capmgr_app_info_h remote_app_info,
    capmgr_device_h* device) {
  if (!remote_app_info || !device)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  int ret = capmgr_device_clone(remote_app_info->device, device);
  if (ret != CAPMGR_ERROR_NONE)
    return ret;

  return CAPMGR_ERROR_NONE;
}

API int capmgr_app_info_foreach_application(
    const capmgr_device_h device,
    capmgr_app_info_foreach_app_cb cb,
    void* user_data) {
  if (!device || !cb)
    return CAPMGR_ERROR_INVALID_PARAMETER;

  std::unique_ptr<capmgr::SQLConnection> sql_conn(
      new capmgr::SQLiteConnection(kDBPath, true));

  const char kQueryForeachPackages[] =
      "SELECT appid, pkgid, label, version FROM remote_app_info WHERE "
      "device_id = ?";

  std::shared_ptr<capmgr::SQLStatement> stmt = sql_conn->PrepareStatement(
      kQueryForeachPackages);
  if (!stmt)
    return CAPMGR_ERROR_IO_ERROR;

  if (stmt->BindString(1, device->device_id)) {
    while (stmt->Step() == capmgr::SQLStatement::StepResult::ROW) {
      struct capmgr_app_info_s info;
      int idx = 0;
      info.appid = stmt->GetColumnString(idx++);
      info.pkgid = stmt->GetColumnString(idx++);
      info.label = stmt->GetColumnString(idx++);
      info.version = stmt->GetColumnString(idx++);
      info.device = device;
      if (cb(&info, user_data))
        break;
    }
  }

  return CAPMGR_ERROR_NONE;
}