Example #1
0
void primary_all_cb(GSList *services, guint8 status,
                    gpointer user_data)
{
  GSList *l = NULL;
  GSList *bl_primary_list = NULL;

  printf_dbg("[CB] IN Primary_all_cb\n");
  if (status) {
    cb_ret_val = BL_REQUEST_FAIL_ERROR;
    sprintf(cb_ret_msg, "Primary callback: Failure: %s\n",
            att_ecode2str(status));
    goto error;
  }

  if (services == NULL) {
    cb_ret_val = BL_NO_ERROR;
    strcpy(cb_ret_msg, "Primary callback: Nothing found\n");
    goto exit;
  }

  for (l = services; l; l = l->next) {
    struct gatt_primary *prim = l->data;
    bl_primary_t *bl_primary = bl_primary_new(prim->uuid, prim->changed,
        prim->range.start, prim->range.end);

    g_free(prim);

    if (bl_primary == NULL) {
      cb_ret_val = BL_MALLOC_ERROR;
      strcpy(cb_ret_msg, "Primary callback: Malloc error\n");
      goto error;
    }
    if (bl_primary_list == NULL) {
      bl_primary_list = g_slist_alloc();
      if (bl_primary_list == NULL) {
        cb_ret_val = BL_MALLOC_ERROR;
        strcpy(cb_ret_msg, "Primary callback: Malloc error\n");
        goto error;
      }
      bl_primary_list->data = bl_primary;
    } else {
      bl_primary_list = g_slist_append(bl_primary_list, bl_primary);
    }
  }

  cb_ret_val = BL_NO_ERROR;
  cb_ret_pointer = bl_primary_list;
  strcpy(cb_ret_msg, "Primary callback: Sucess\n");
  goto exit;

 error:
  if (bl_primary_list)
    bl_primary_list_free(bl_primary_list);
 exit:
  if (l)
    g_slist_free(l);
  g_mutex_unlock(pending_callback);
  printf_dbg("[CB] OUT primary_all_cb\n");
}
Example #2
0
void primary_by_uuid_cb(GSList *ranges, guint8 status,
                        gpointer user_data)
{
    GSList   *l;
    GSList   *bl_primary_list = NULL;
    cb_ctx_t *cb_ctx = user_data;

    printf_dbg("IN primary_by_uuid_cb\n");
    if (status) {
        cb_ctx->cb_ret_val = BL_REQUEST_FAIL_ERROR;
        sprintf(cb_ctx->cb_ret_msg, "Primary by UUID callback: Failure: %s\n",
                att_ecode2str(status));
        goto error;
    }
    if (ranges == NULL) {
        cb_ctx->cb_ret_val = BL_NO_ERROR;
        strcpy(cb_ctx->cb_ret_msg,
               "Primary by UUID callback: Nothing found\n");
        goto exit;
    }

    for (l = ranges; l; l = l->next) {
        struct att_range *range = l->data;
        bl_primary_t *bl_primary = bl_primary_new(NULL, 0, range->start,
                                                  range->end);
        free(range);

        if (bl_primary == NULL) {
            cb_ctx->cb_ret_val = BL_MALLOC_ERROR;
            strcpy(cb_ctx->cb_ret_msg,
                   "Primary by UUID callback: Malloc error\n");
            goto error;
        }
        if (bl_primary_list == NULL) {
            bl_primary_list = g_slist_alloc();

            if (bl_primary_list == NULL) {
                cb_ctx->cb_ret_val = BL_MALLOC_ERROR;
                strcpy(cb_ctx->cb_ret_msg,
                       "Primary by UUID callback: Malloc error\n");
                goto error;
            }
            bl_primary_list->data = bl_primary;
        } else {
            bl_primary_list = g_slist_append(bl_primary_list, bl_primary);
        }
    }
    cb_ctx->cb_ret_val = BL_NO_ERROR;
    cb_ctx->cb_ret_pointer = bl_primary_list;
    goto exit;

error:
    if (bl_primary_list)
        bl_primary_list_free(bl_primary_list);
exit:
    g_mutex_unlock(&cb_ctx->pending_cb_mtx);
    printf_dbg("OUT primary_by_uuid_cb\n");
}
Example #3
0
static int get_ble_tree(const int thd_nb, char *mac,
                        const char *file_path)
{

    int      ret_int;
    GError  *gerr             = NULL;
    GSList  *bl_primary_list  = NULL;
    GSList  *bl_included_list = NULL;
    GSList  *bl_char_list     = NULL;
    GSList  *bl_desc_list     = NULL;
    bl_value_t  *bl_value     = NULL;

    FILE  *file = fopen(file_path, "w");
    if (!file)
        return -1;
    // Initialisation
    bl_init(&gerr);
    if (check_gerrors(thd_nb, gerr)) {
        printf("[THD%d] ERROR: Unable to initalise BlueLib\n", thd_nb);
        return -1;
    }
    dev_init(&dev_ctx, NULL, mac, NULL, 0, TEST_SEC_LEVEL);

    do {
        ret_int = bl_connect(&dev_ctx);
    } while (check_errors(thd_nb, ret_int));

    if (ret_int)
        return -1;

    do {
        bl_value = bl_read_char(&dev_ctx, GATT_CHARAC_DEVICE_NAME_STR, NULL,
                                &gerr);
    } while(check_gerrors(thd_nb, gerr));

    printf("[THD%d] In progress\n", thd_nb);
    if (bl_value) {
        char device_name_str[bl_value->data_size + 1];
        memcpy(device_name_str, bl_value->data,
               bl_value->data_size);
        device_name_str[bl_value->data_size] = '\0';
        fprintf(file, "Device name: %s\n", device_name_str);
        bl_value_free(bl_value);
    } else {
        printf("[THD%d] Impossible to retrieve the name of the deviceĀ \n",
               thd_nb);
        goto disconnect;
    }

    fprintf(file, "Handle |\n");

    do {
        bl_primary_list  = bl_get_all_primary(&dev_ctx, NULL, &gerr);
    } while (check_gerrors(thd_nb, gerr));

    printf("[THD%d].", thd_nb);

    if (bl_primary_list) {
        for (GSList *lp = bl_primary_list; lp; lp = lp->next) {
            printf(".");
            bl_primary_t *bl_primary = lp->data;

            bl_primary_fprint(file, lp->data);

            // Get all included in the primary service
            do {
                bl_included_list = bl_get_included(&dev_ctx, bl_primary,
                                                   &gerr);
            } while (check_gerrors(thd_nb, gerr));

            if (bl_included_list) {
                bl_included_list_fprint(file, bl_included_list);
                fprintf(file, "       |\n");
                bl_included_list_free(bl_included_list);
            }

            // Get all characteristics in the primary service
            do {
                bl_char_list = bl_get_all_char_in_primary(&dev_ctx,
                                                          bl_primary, &gerr);
            } while (check_gerrors(thd_nb, gerr));

            if (bl_char_list) {
                for (GSList *lc = bl_char_list; lc; lc = lc->next) {
                    putchar('.');
                    bl_char_t *bl_char = lc->data;

                    bl_char_fprint(file, bl_char);

                    // Get all descriptors of the characteristic
                    {
                        bl_char_t *next_bl_char = NULL;
                        if (lc->next)
                            next_bl_char = lc->next->data;
                        do {
                            bl_desc_list =
                                bl_get_all_desc_by_char(&dev_ctx, bl_char,
                                                        next_bl_char,
                                                        bl_primary, &gerr);
                        } while (check_gerrors(thd_nb, gerr));
                    }
                    if (bl_desc_list)
                        bl_desc_list_fprint(file, bl_desc_list);
                    bl_desc_list_free(bl_desc_list);
                    if (lc->next)
                        fprintf(file, "       | |\n");
                }
                bl_char_list_free(bl_char_list);
            }
            if (lp->next)
                fprintf(file, "       |\n");
        }
        bl_primary_list_free(bl_primary_list);
    }
    printf("[THD%d] All done!\n", thd_nb);

disconnect:
    if (file)
        fclose(file);
    printf("[THD%d] Disconnecting\n", thd_nb);
    bl_disconnect(&dev_ctx);
    bl_stop();
    return 0;
}