static void get_buf_xfer_progress(GObex *obex, GError *err, GObexPacket *rsp, gpointer user_data) { struct obc_transfer *transfer = user_data; struct transfer_callback *callback = transfer->callback; GObexPacket *req; GObexHeader *hdr; const guint8 *buf; gsize len; guint8 rspcode; gboolean final; if (err != NULL) { get_buf_xfer_complete(obex, err, transfer); return; } rspcode = g_obex_packet_get_operation(rsp, &final); if (rspcode != G_OBEX_RSP_SUCCESS && rspcode != G_OBEX_RSP_CONTINUE) { err = g_error_new(OBEX_IO_ERROR, rspcode, "Transfer failed (0x%02x)", rspcode); get_buf_xfer_complete(obex, err, transfer); g_error_free(err); return; } hdr = g_obex_packet_get_header(rsp, G_OBEX_HDR_APPARAM); if (hdr) { g_obex_header_get_bytes(hdr, &buf, &len); if (len != 0) { transfer->params->data = g_memdup(buf, len); transfer->params->size = len; } } hdr = g_obex_packet_get_body(rsp); if (hdr) { g_obex_header_get_bytes(hdr, &buf, &len); if (len != 0) obc_transfer_read(transfer, buf, len); } if (rspcode == G_OBEX_RSP_SUCCESS) { get_buf_xfer_complete(obex, err, transfer); return; } req = g_obex_packet_new(G_OBEX_OP_GET, TRUE, G_OBEX_HDR_INVALID); transfer->xfer = g_obex_send_req(obex, req, -1, get_buf_xfer_progress, transfer, &err); if (callback) callback->func(transfer, transfer->transferred, err, callback->data); }
GObexApparam *g_obex_header_get_apparam(GObexHeader *header) { gboolean ret; const guint8 *val; gsize len; ret = g_obex_header_get_bytes(header, &val, &len); if (!ret) return NULL; return g_obex_apparam_decode(val, len); }
static void handle_get(GObex *obex, GObexPacket *req, gpointer user_data) { GError *err = NULL; struct transfer_data *data; const char *type, *name; GObexHeader *hdr; gsize type_len; hdr = g_obex_packet_get_header(req, G_OBEX_HDR_TYPE); if (hdr != NULL) { g_obex_header_get_bytes(hdr, (const guint8 **) &type, &type_len); if (type[type_len - 1] != '\0') { g_printerr("non-nul terminated type header\n"); type = NULL; } } else type = NULL; hdr = g_obex_packet_get_header(req, G_OBEX_HDR_NAME); if (hdr != NULL) g_obex_header_get_unicode(hdr, &name); else name = NULL; g_print("get type \"%s\" name \"%s\"\n", type ? type : "", name ? name : ""); data = g_new0(struct transfer_data, 1); data->fd = open(name, O_RDONLY | O_NOCTTY, 0); if (data->fd < 0) { g_printerr("open(%s): %s\n", name, strerror(errno)); g_free(data); g_obex_send_rsp(obex, G_OBEX_RSP_FORBIDDEN, NULL, G_OBEX_HDR_INVALID); return; } g_obex_get_rsp(obex, send_data, transfer_complete, data, &err, G_OBEX_HDR_INVALID); if (err != NULL) { g_printerr("Unable to send response: %s\n", err->message); g_error_free(err); g_free(data); } }
static void test_header_encode_body(void) { GObexHeader *header; guint8 expected[] = { 1, 2, 3, 4}; const guint8 *buf; size_t len; gboolean ret; header = parse_and_encode(hdr_body, sizeof(hdr_body)); ret = g_obex_header_get_bytes(header, &buf, &len); g_assert(ret == TRUE); assert_memequal(expected, sizeof(expected), buf, len); g_obex_header_free(header); }
static gboolean handle_get_body(struct transfer *transfer, GObexPacket *rsp, GError **err) { GObexHeader *body = g_obex_packet_get_body(rsp); gboolean ret; const guint8 *buf; gsize len; if (body == NULL) return TRUE; g_obex_header_get_bytes(body, &buf, &len); if (len == 0) return TRUE; ret = transfer->data_consumer(buf, len, transfer->user_data); if (ret == FALSE) g_set_error(err, G_OBEX_ERROR, G_OBEX_ERROR_CANCELLED, "Data consumer callback failed"); return ret; }
static void test_decode_connect(void) { GObexPacket *pkt; GObexHeader *header; GError *err = NULL; gboolean ret; const guint8 *buf; guint8 target[] = { 0xab, 0xcd }; gsize len; pkt = g_obex_packet_decode(pkt_connect, sizeof(pkt_connect), 4, G_OBEX_DATA_REF, &err); g_assert_no_error(err); g_assert(pkt != NULL); header = g_obex_packet_get_header(pkt, G_OBEX_HDR_TARGET); g_assert(header != NULL); ret = g_obex_header_get_bytes(header, &buf, &len); g_assert(ret == TRUE); assert_memequal(target, sizeof(target), buf, len); g_obex_packet_free(pkt); }