irpc_retval_t irpc_recv_usb_get_string_descriptor_ascii(struct irpc_connection_info *ci, irpc_device_handle *handle, int idx, char data[], int length) { tpl_node *tn = NULL; int retval; irpc_func_t func = IRPC_USB_GET_STRING_DESCRIPTOR_ASCII; int sock = ci->server_sock; irpc_send_func(func, sock); tn = tpl_map(IRPC_STRING_DESC_FMT, handle, &idx, &length); tpl_pack(tn, 0); tpl_dump(tn, TPL_FD, sock); tpl_free(tn); tn = tpl_map(IRPC_STR_INT_FMT, &retval, data, IRPC_MAX_DATA); tpl_load(tn, TPL_FD, sock); tpl_unpack(tn, 0); tpl_free(tn); return retval; }
int main() { struct st dst, s = {'a', {0,1,2,3,4,5,6,7,8,9}}; tpl_node *tn; int j; tn = tpl_map("A(S(ci#))", &s, ILEN); tpl_pack(tn,1); /* fiddle with the fields and pack another element */ s.c++; for(j=0;j<ILEN;j++) s.i[j]++; tpl_pack(tn,1); tpl_dump(tn,TPL_FILE,"/tmp/test81.tpl"); tpl_free(tn); tn = tpl_map("A(S(ci#))", &dst, ILEN); tpl_load(tn,TPL_FILE,"/tmp/test81.tpl"); while (tpl_unpack(tn,1) > 0) { printf("%c ", dst.c); for(j=0;j<ILEN;j++) printf("%d ", dst.i[j]); printf("\n"); } tpl_free(tn); return(0); }
void irpc_send_usb_bulk_transfer(struct irpc_connection_info *ci) { tpl_node *tn = NULL; irpc_retval_t retval = IRPC_SUCCESS; irpc_device_handle handle; char endpoint, data[IRPC_MAX_DATA];; int length, transfered, timeout; int sock = ci->client_sock; tn = tpl_map(IRPC_BULK_TRANSFER_FMT, &handle, &endpoint, &length, &transfered, &timeout); tpl_load(tn, TPL_FD, sock); tpl_unpack(tn, 0); tpl_free(tn); retval = libusb_bulk_transfer(irpc_handle, endpoint, data, length, &transfered, timeout); // Send libusb_bulk_transfer packet. tn = tpl_map(IRPC_STR_INT_INT_FMT, &retval, &transfered, &data, IRPC_MAX_DATA); tpl_pack(tn, 0); tpl_dump(tn, TPL_FD, sock); tpl_free(tn); }
void ClientMapPacker::unpackFromNet(ClientMap *client, unsigned char *buf, GraphicsTCOD *screen) { clientMap = client; s_render_t sMap; tn = tpl_map((char *)"S(iiifffi)", &sMap); if(!tpl_load(tn, TPL_MEM | TPL_EXCESS_OK, buf, TILE_PACKET_SIZE)) { tpl_unpack(tn, 0); clientMap->refreshSquare(sMap.x, sMap.y); rMap = clientMap->cMap[sMap.x][sMap.y]; render_t cMap = serialToClient(sMap); clientMap->cMap[cMap.x][cMap.y]->x = cMap.x; clientMap->cMap[cMap.x][cMap.y]->y = cMap.y; clientMap->cMap[cMap.x][cMap.y]->H = cMap.H; clientMap->cMap[cMap.x][cMap.y]->S = cMap.S; clientMap->cMap[cMap.x][cMap.y]->V = cMap.V; clientMap->cMap[cMap.x][cMap.y]->explored = cMap.explored; clientMap->cMap[cMap.x][cMap.y]->visible = cMap.visible; clientMap->cMap[cMap.x][cMap.y]->drawn = false; } tpl_free(tn); }
irpc_retval_t irpc_recv_usb_clear_halt(struct irpc_connection_info *ci, irpc_device_handle *handle, char endpoint) { tpl_node *tn = NULL; irpc_retval_t retval; irpc_func_t func = IRPC_USB_CLEAR_HALT; int sock = ci->server_sock; irpc_send_func(func, sock); // Send irpc_device_handle, and endpoint to server. tn = tpl_map(IRPC_CLEAR_HALT_FMT, handle, &endpoint); tpl_pack(tn, 0); tpl_dump(tn, TPL_FD, sock); tpl_free(tn); // Read libusb_clear_halt packet. tn = tpl_map(IRPC_INT_FMT, &retval); tpl_load(tn, TPL_FD, sock); tpl_unpack(tn, 0); tpl_free(tn); return retval; }
int main(int argc, char *argv[]) { char *prog = argv[0]; int opt, rc=-1; hex_t h; utarray_new(cfg.hexv, &hex_icd); while ( (opt = getopt(argc, argv, "v+i:o:h")) != -1) { switch (opt) { case 'v': cfg.verbose++; break; case 'i': cfg.infile = strdup(optarg); break; case 'o': cfg.outfile = strdup(optarg); break; case 'h': default: usage(prog); break; } } if (!cfg.outfile || !cfg.infile) usage(prog); /* read input file */ tpl_node *tn = tpl_map("A(sii)", &h.id, &h.x, &h.y); if (tpl_load(tn, TPL_FILE, cfg.infile)) goto done; while(tpl_unpack(tn,1) > 0) utarray_push_back(cfg.hexv, &h); tpl_free(tn); find_bounds(); draw_image(); rc = 0; done: utarray_free(cfg.hexv); return rc; }
irpc_retval_t irpc_recv_usb_bulk_transfer(struct irpc_connection_info *ci, irpc_device_handle *handle, char endpoint, char data[], int length, int *transfered, int timeout) { tpl_node *tn = NULL; int retval; irpc_func_t func = IRPC_USB_BULK_TRANSFER; int sock = ci->server_sock; irpc_send_func(func, sock); tn = tpl_map(IRPC_BULK_TRANSFER_FMT, handle, &endpoint, &length, transfered, &timeout); tpl_pack(tn, 0); tpl_dump(tn, TPL_FD, sock); tpl_free(tn); // Read libusb_bulk_transfer packet. tn = tpl_map(IRPC_STR_INT_INT_FMT, &retval, &transfered, data, IRPC_MAX_DATA); tpl_load(tn, TPL_FD, sock); tpl_unpack(tn, 0); tpl_free(tn); return retval; }
gesture deserialize_gesture(void* buffer, size_t bufsize) { gesture g; tpl_node *tn; tn = tpl_map(gesture_format_string, &g); tpl_load(tn, TPL_MEM, buffer, bufsize); tpl_unpack(tn, 0); tpl_free(tn); return g; }
void irpc_send_usb_open(struct irpc_connection_info *ci) { tpl_node *tn = NULL; irpc_retval_t retval = IRPC_SUCCESS; irpc_device idev; libusb_device *f = NULL; libusb_device **list = NULL; irpc_device_handle ihandle; int sock = ci->client_sock; // Read irpc_device from client. tn = tpl_map(IRPC_DEV_FMT, &idev); tpl_load(tn, TPL_FD, sock); tpl_unpack(tn, 0); tpl_free(tn); int i; ssize_t cnt = libusb_get_device_list(irpc_ctx, &list); for (i = 0; i < cnt; i++) { libusb_device *dev = list[i]; if (dev->session_data == idev.session_data) { f = dev; break; } } if (!f) { retval = IRPC_FAILURE; goto send; } // Close an already opened handle. if (irpc_handle) { libusb_close(irpc_handle); irpc_handle = NULL; } if (libusb_open(f, &irpc_handle) != 0) { retval = IRPC_FAILURE; goto send; } libusb_free_device_list(list, 1); ihandle.dev.bus_number = irpc_handle->dev->bus_number; ihandle.dev.device_address = irpc_handle->dev->device_address; ihandle.dev.num_configurations = irpc_handle->dev->num_configurations; ihandle.dev.session_data = irpc_handle->dev->session_data; send: // Send libusb_open packet. tn = tpl_map(IRPC_DEV_HANDLE_RET_FMT, &ihandle, &retval); tpl_pack(tn, 0); tpl_dump(tn, TPL_FD, sock); tpl_free(tn); }
int main() { tpl_node *tn; int i; tn = tpl_map("A(i)",&i); for(i=0;i<10;i++) tpl_pack(tn,1); /* test pack-then-unpack without dump/load; implicit dump/load*/ while (tpl_unpack(tn,1) > 0) printf("i is %d\n", i); /* implicit conversion back to output tpl (discards previous data in tpl */ for(i=0;i>-10;i--) tpl_pack(tn,1); /* one more implicit conversion */ while (tpl_unpack(tn,1) > 0) printf("i is %d\n", i); tpl_free(tn); return(0); }
int DecodeMsg(struct Msg* messageStruct, place_t _rec_from) { int fdToReadFrom = _get_read_fd(_rec_from); tpl_node * tn; tpl_bin tb; //struct MsgRead* msgStruct = (struct MsgRead*)malloc(sizeof(struct MsgRead)); tn = tpl_map("S(iiU)B", messageStruct, &tb); //writeDebug("MSG Mapped"); int validity = (fcntl(fdToReadFrom, F_GETFL) != -1); if (validity) { writeDebugExtraPlace("File descriptor is accessible", fdToReadFrom); } else { writeErrorExtraPlace("File descriptor is not accessible", fdToReadFrom); } //writeDebug("started trying to load msg"); int success = tpl_load(tn, TPL_FD, fdToReadFrom); //writeDebug("Finished loading"); if (success == 0) { writeDebugExtraPlace("msg read on file descriptor", fdToReadFrom); } else { writeErrorExtraPlace("msg failed to read on file descriptor", fdToReadFrom); return EXIT_FAILURE; } //writeDebug("unpacking reading msg"); tpl_unpack(tn, 0); //writeDebug("msg unpacked succesfully"); //messageStruct->msgType = msgStruct->msgType; //messageStruct->placeFrom = msgStruct->placeFrom; //messageStruct->size = msgStruct->size; messageStruct->tb.addr = tb.addr; messageStruct->tb.sz = tb.sz; writeDebugMsg(messageStruct); //writeDebug("tpl will be freed"); tpl_free(tn); //writeDebug("tpl freed"); if (success) { return EXIT_SUCCESS; } else { return EXIT_FAILURE; } }
int32_t DecodeMsg(struct Msg* messageStruct, place_t _rec_from) { int32_t fdToReadFrom = _get_read_fd(_rec_from); tpl_node * tn; tpl_bin tb; //struct MsgRead* msgStruct = (struct MsgRead*)GC_MALLOC(sizeof(struct MsgRead)); tn = tpl_map("S(iiiU)B", messageStruct, &tb); //writeDebug("MSG Mapped"); int32_t validity = (fcntl(fdToReadFrom, F_GETFL) != -1); if (validity) { //writeDebugExtraPlace("File descriptor is accessible", //fdToReadFrom); } else { writeErrorExtraPlace("File descriptor is not accessible", fdToReadFrom); } //writeDebug("started trying to load msg"); int32_t success = tpl_load(tn, TPL_FD, fdToReadFrom); //writeDebug("Finished loading"); if (success == 0) { //writeDebugExtraPlace("msg read on file descriptor", //fdToReadFrom); } else { writeErrorExtraPlace("msg failed to read on file descriptor", fdToReadFrom); return EXIT_FAILURE; } //writeDebug("unpacking reading msg"); tpl_unpack(tn, 0); //writeDebug("msg unpacked succesfully"); //messageStruct->msgType = msgStruct->msgType; //messageStruct->placeFrom = msgStruct->placeFrom; //messageStruct->size = msgStruct->size; //messageStruct->placeTo = msgStruct->placeTo; messageStruct->tb.addr = tb.addr; messageStruct->tb.sz = tb.sz; // uint64_t temp2 = getMicroTime(); // int64_t temp = (temp2-(messageStruct->time)); // fprintf(stderr, "%d GOING TO %d TOTAL TIME THIS MESSAGE TOOK TIME: %lld, %llu, %llu \n", messageStruct->placeFrom, messageStruct->placeTo, temp, temp2, messageStruct->time); // writeDebugMsg(messageStruct); //fprintf(stderr, "%d:blob READ iuuubefore %s make sure msg is correct \n", _here(), (char *)tb.addr); //writeDebug("tpl will be freed"); tpl_free(tn); return EXIT_SUCCESS; //writeDebug("tpl freed"); }
irpc_func_t irpc_read_func(int sock) { irpc_func_t func; tpl_node *tn = tpl_map(IRPC_INT_FMT, &func); tpl_load(tn, TPL_FD, sock); tpl_unpack(tn, 0); tpl_free(tn); return func; }
int main() { tpl_node *tn; int i; tpl_hook.oops = printf; tn = tpl_map("A(i)",&i); tpl_load(tn,TPL_FILE,"test13.tpl"); while (tpl_unpack(tn,1) > 0) printf("i is %d\n", i); tpl_free(tn); return(0); }
static int tpl_deserialize_array(tpl_node *stn, tpl_node *rtn, int cb, char *c, char *buf) { if (cb > 1024) { fprintf(stderr, "big tpl_load %d\n", cb); if (tpl_load(rtn, TPL_MEM|TPL_EXCESS_OK, _big_buf + H_OFFSET, cb - H_OFFSET) < 0) { fprintf(stderr, "load failed\n"); tpl_free(stn); tpl_free(rtn); return 0; } } else { if (tpl_load(rtn, TPL_MEM|TPL_EXCESS_OK, _buf + H_OFFSET, sizeof(_buf) - H_OFFSET) < 0) { fprintf(stderr, "load failed\n"); tpl_free(stn); tpl_free(rtn); return 0; } } tpl_unpack(rtn, 0); int i = 0; while (tpl_unpack(rtn, 1) > 0) { buf[i] = *c; i++; } tpl_free(stn); tpl_free(rtn); if (cb > 1020) { free(_big_buf); } return i; }
static int get(void * buffer, int size, int * op, char ** name) { tpl_node * node = NULL; node = tpl_map("is", op, name); if(tpl_load(node, TPL_MEM, buffer, size) == -1) { tpl_free(node); return FALSE; } tpl_unpack(node, 0); tpl_free(node); return TRUE; }
int tpl_cb(void *tpl, size_t tpllen, void*data) { int i; tpl_node *tn; if (DEBUG) printf("obtained tpl of length %d\n", (int)tpllen); tn = tpl_map("A(i)", &i); tpl_load(tn, TPL_MEM, tpl, tpllen); num_tpls++; while (tpl_unpack(tn,1) > 0) sum_tpls += i; tpl_free(tn); return 0; }
DLPSPEC_ERR_CODE dlpspec_deserialize(void* struct_p, const size_t buffer_size, BLOB_TYPES data_type) /** * Deserializes a blob in place for the passed in struct pointer @p struct_p. * Upon return, @p struct_p holds a struct of the type determined by @p data_type. * * @param[in,out] struct_p pointer to the blob of data, returned as applicable struct. * @param[in] buffer_size size allocated to blob * @param[in] data_type blob type to be deserialized in place * * @return Error code * */ { tpl_node *tn; DLPSPEC_ERR_CODE ret_val = DLPSPEC_PASS; int tpl_err = 0; tn = map_data_to_tplnode(struct_p, data_type); if(tn != NULL) { #ifdef NANO_PRE_1_1_8_BLE_WORKAROUND // If 'tpl\0' was overwritten by BLE in DLP NIRscan Nano firmware // ≤ v1.1.7, try unpacking anyway if ((data_type == REF_CAL_MATRIX_TYPE) || (data_type == SCAN_DATA_TYPE)) { const char tpl_header[] = "tpl\0"; memcpy(struct_p, tpl_header, sizeof(tpl_header)); } #endif // unpack data tpl_err = tpl_load(tn, TPL_MEM|TPL_EXCESS_OK, struct_p, buffer_size); if ( 0 != tpl_err) // TPL error in loading { ret_val = ERR_DLPSPEC_TPL; } else { tpl_err = tpl_unpack(tn, 0); } tpl_free(tn); } else { ret_val = ERR_DLPSPEC_TPL; } return ret_val; }
int main() { tpl_node *tn; int o,i,j=-1; void *addr; int sz; tn = tpl_map("A(A(i))",&i); for(o=0;o<10;o++) { for(i=o; i < o+10; i++) tpl_pack(tn,2); tpl_pack(tn,1); } tpl_dump(tn,TPL_MEM,&addr,&sz); tpl_free(tn); tn = tpl_map("A(A(i))",&j); tpl_load(tn,TPL_MEM,addr,sz); while (tpl_unpack(tn,1) > 0) { while (tpl_unpack(tn,2) > 0) printf("j is %d\n", j); } tpl_free(tn); free(addr); return(0); }
static void server_loop(int sock) { struct timeval oneminute = { 60, 0 }; fd_set sockset; int minutes_idle = 0; // accepting and dispatching messages for (;;) { int msg_type; tpl_node *tn; int incoming; int maxfd, result; FD_ZERO(&sockset); FD_SET(sock, &sockset); maxfd = sock; result = select(maxfd+1, &sockset, 0, 0, &oneminute); if (!result) { minutes_idle++; if (minutes_idle >= AUTO_SHUTDOWN_TIME) return; continue; } minutes_idle = 0; incoming = accept(sock, 0, 0); if (incoming == -1) { fprintf(stderr, "Error! Failed to accept an incoming connection.\n"); exit(1); } tn = tpl_map("i", &msg_type); tpl_load(tn, TPL_FD, incoming); tpl_unpack(tn, 0); tpl_free(tn); switch (msg_type) { case MSG_CLOSE: close(incoming); return; case MSG_AC: process_ac(incoming); break; default: ; } close(incoming); } }
int main(int argc, char * argv[]) { int opt,verbose=0, dump_image=0, x, y; FILE *ifilef=stdin; char *ifile=NULL,line[100], *s; tpl_node *tn; char *buf, *obuf; size_t sz, osz; while ( (opt = getopt(argc, argv, "v+ih")) != -1) { switch (opt) { case 'v': verbose++; break; case 'i': dump_image=1; break; case 'h': default: usage(argv[0]); break; } } if (optind < argc) ifile=argv[optind++]; if (ifile) { if ( (ifilef = fopen(ifile,"r")) == NULL) { fprintf(stderr,"can't open %s: %s\n", ifile, strerror(errno)); exit(-1); } } /* prepare input buffer for layout algorithm */ s = line; tn = tpl_map("A(s)", &s); while (fgets(line,sizeof(line),ifilef) != NULL) tpl_pack(tn, 1); tpl_dump(tn, TPL_MEM, &buf, &sz); tpl_free(tn); /* perform layout */ layout(buf,sz,&obuf,&osz); free(buf); /* inspect output buffer from layout algorithm */ tn = tpl_map("A(sii)", &s, &x, &y); if (tpl_load(tn, TPL_MEM, obuf, osz)) exit(-1); while (tpl_unpack(tn,1) > 0) { if (verbose) fprintf(stderr,"%-5d %-5d %s", x, y, s); free(s); } tpl_free(tn); if (dump_image) write(STDOUT_FILENO, obuf, osz); free(obuf); fclose(ifilef); return 0; }
int handle_list(struct opcode_context *ctx) { tpl_node *packet; char *peer_id = NULL, *peer_address = NULL; unsigned short peer_port = 0; packet = tpl_map(LIST_TPL_FORMAT, &peer_id, &peer_address, &peer_port); tpl_load(packet, TPL_MEM|TPL_PREALLOCD|TPL_EXCESS_OK, ctx->p_ctx->message, ctx->p_ctx->message_len); while (tpl_unpack(packet, 1) > 0) { printf("Read: %s %s %d\n", peer_id, peer_address, peer_port); } tpl_free(packet); return RESPONSE_HANDLED; }
void Load(char* szFileName) { tpl_node *tn; int id; char *name; tn = tpl_map("A(is)", &id, &name); tpl_load(tn, TPL_FILE, szFileName); while (tpl_unpack(tn, 1) > 0) { printf("id %d, user %s\n", id, name); free(name); } tpl_free(tn); }
int main(int argc, char *argv[]) { tpl_node *tn; int id; char *name; tn = tpl_map("A(is)", &id, &name); tpl_load(tn, TPL_FILE, "/tmp/test35.tpl"); while ( tpl_unpack(tn,1) > 0 ) { printf("id %d, user %s\n", id, name); free(name); } tpl_free(tn); return(0); }
int main() { tpl_node *tn; char *s = NULL; tn = tpl_map("s", &s); tpl_pack(tn,0); tpl_dump(tn,TPL_FILE,filename); tpl_free(tn); s = (char*)0x1; /* overwritten below */ tn = tpl_map("s", &s); tpl_load(tn,TPL_FILE,filename); tpl_unpack(tn,0); tpl_free(tn); printf("s %s null\n", (s==NULL?"is":"is NOT")); return(0); }
static int tpl_deserialize(tpl_node *stn, tpl_node *rtn) { if (tpl_load(rtn, TPL_MEM|TPL_EXCESS_OK, _buf + H_OFFSET, sizeof(_buf) - H_OFFSET) < 0) { fprintf(stderr, "load failed\n"); tpl_free(stn); tpl_free(rtn); return -1; } tpl_unpack(rtn, 0); tpl_free(stn); tpl_free(rtn); return 0; }
int layout(char *buf, size_t sz, char **obuf, size_t *osz) { UT_array *namev,*hashv, *xv, *yv; int i, pos, rc=-1, x, y; unsigned char *s; unsigned h; utarray_new(namev, &ut_str_icd); utarray_new(hashv, &ut_int_icd); utarray_new(xv, &ut_int_icd); utarray_new(yv, &ut_int_icd); tpl_node *tn = tpl_map("A(s)", &s); if (tpl_load(tn, TPL_MEM, buf, sz)) goto done; while (tpl_unpack(tn,1) > 0) { h = get_hash(s); utarray_push_back(namev, &s); utarray_push_back(hashv, &h); free(s); } tpl_free(tn); for(pos=0; pos < utarray_len(namev); pos++) { place(pos,hashv,xv,yv); } /* every item has a place. prepare output. */ tn = tpl_map("A(sii)", &s, &x, &y); for(i=0; i < utarray_len(namev); i++) { s = *(unsigned char**)utarray_eltptr(namev,i); x = *(int*)utarray_eltptr(xv,i); y = *(int*)utarray_eltptr(yv,i); tpl_pack(tn,1); } tpl_dump(tn, TPL_MEM, obuf, osz); tpl_free(tn); rc = 0; done: utarray_free(namev); utarray_free(hashv); utarray_free(xv); utarray_free(yv); return rc; }
/* encode a tpl (kv frame) as json. */ void *enc_worker(void *thread_id) { char buf[MAX_BUF], *key, *val; json_t *o = json_object(); int rc=-1, len, nc; tpl_node *tn; while (CF.shutdown == 0) { len = nn_recv(CF.ingress_socket_pull, buf, MAX_BUF, 0); if (len < 0) { fprintf(stderr,"nn_recv: %s\n", nn_strerror(errno)); goto done; } /* decode, then re-encode as json */ json_object_clear(o); tn = tpl_map("A(ss)",&key,&val); assert(tn); if (tpl_load(tn,TPL_MEM,buf,len) < 0) goto done; while(tpl_unpack(tn,1) > 0) { json_t *jval = json_string(val); json_object_set_new(o, key, jval); free(key); key=NULL; free(val); val=NULL; } tpl_free(tn); /* dump the json object, then newline-terminate it. */ if (CF.verbose>1) json_dumpf(o, stderr, JSON_INDENT(1)); char *dump = json_dumps(o, JSON_INDENT(0)); size_t dump_len = strlen(dump); /* give the buffer to nano, from here it goes to kaf thread */ nc = nn_send(CF.egress_socket_push, dump, dump_len, 0); free(dump); if (nc < 0) { fprintf(stderr,"nn_send: %s\n", nn_strerror(errno)); goto done; } } rc = 0; done: CF.shutdown = 1; json_decref(o); return NULL; }
int tpl_cb(void *tpl, size_t tpllen, void*data) { int i; tpl_node *tn; tpl_hook.oops = printf; if (DEBUG) printf("obtained tpl of length %d\n", (int)tpllen); tn = tpl_map("A(i)", &i); tpl_load(tn, TPL_MEM, tpl, tpllen); num_tpls++; while (tpl_unpack(tn,1) > 0) sum_tpls += i; tpl_free(tn); /* this next line is a hack to test the callback's ability * to abort further tpl processing by returning < 0 */ if (num_tpls == 3) return -1; return 0; }
irpc_retval_t irpc_recv_usb_init(struct irpc_connection_info *ci) { tpl_node *tn = NULL; irpc_retval_t retval = IRPC_FAILURE; irpc_func_t func = IRPC_USB_INIT; int sock = ci->server_sock; irpc_send_func(func, sock); // Read usb_init packet. tn = tpl_map(IRPC_INT_FMT, &retval); tpl_load(tn, TPL_FD, sock); tpl_unpack(tn, 0); tpl_free(tn); return retval; }