📄 obex.c
字号:
return 0;}/* * IrOBEX_TransportConnect - Connect Irda transport * @self: OBEX handle * @service: IrIAS service name to connect to * * An easier connect function to use for IrDA (IrOBEX) only. */gint IrOBEX_TransportConnect(obex_t *self, const char *service){ DEBUG(4, G_GNUC_FUNCTION "()\n"); if (self->object) { DEBUG(1, G_GNUC_FUNCTION "() We are busy.\n"); return -EBUSY; } g_return_val_if_fail(self != NULL, -1); g_return_val_if_fail(service != NULL, -1);#ifdef HAVE_IRDA irobex_prepare_connect(self, service); return obex_transport_connect_request(self);#else return -ESOCKTNOSUPPORT;#endif /* HAVE_IRDA */}/** * OBEX_GetFD - Get FD * @self: OBEX handle * * Returns the filedescriptor of the transport. Returns -1 on error. * Note that if you for example have a custom transport, no fd * is available. * * The returned filehandle can be used to do select() on, before * calling OBEX_HandleInput() * * There is one subtelty about this function. When the OBEX connection is * established, it returns the connection filedescriptor, while for * an unconnected server it will return the listening filedescriptor. * This mean that after receiving an incomming connection, you need to * call this function again. */gint OBEX_GetFD(obex_t *self){ g_return_val_if_fail(self != NULL, -1); if(self->fd == -1) return self->serverfd; return self->fd;}/** * OBEX_Request - Start a request (as client) * @self: OBEX handle * @object: Object containing request * * Returns negative on error. */gint OBEX_Request(obex_t *self, obex_object_t *object){ DEBUG(4, G_GNUC_FUNCTION "()\n"); if (self->object) { DEBUG(1, G_GNUC_FUNCTION "() We are busy.\n"); return -EBUSY; } g_return_val_if_fail(self != NULL, -1); g_return_val_if_fail(object != NULL, -1); self->object = object; self->state = STATE_START | MODE_CLI; obex_client(self, NULL, 0); return 0;}/** * OBEX_CancelRequest - Cancel an ongoing operation * @self: OBEX handle * @nice: If true an OBEX Abort will be sent if beeing client * or OBEX_RSP_UNAUTHORIZED as reponse if beeing server. * * */gint OBEX_CancelRequest(obex_t *self, gboolean nice){ g_return_val_if_fail(self != NULL, -1); return obex_cancelrequest(self, nice);}/** * OBEX_ObjectNew - Create a new OBEX Object * @self: OBEX handle * @cmd: command of object * * Returns a pointer to a new OBEX Object or %NULL on error. */obex_object_t *OBEX_ObjectNew(obex_t *self, guint8 cmd){ obex_object_t *object; object = obex_object_new(); if(object == NULL) return NULL; obex_object_setcmd(object, cmd, (guint8) (cmd | OBEX_FINAL)); /* Need some special woodoo magic on connect-frame */ if(cmd == OBEX_CMD_CONNECT) { if(obex_insert_connectframe(self, object) < 0) { obex_object_delete(object); object = NULL; } } return object;}/** * OBEX_ObjectDelete - Delete an OBEX object * @self: OBEX handle * @object: object to delete. * * Note that as soon as you have passed an object to the lib using * OBEX_Request(), you shall not delete it yourself. */gint OBEX_ObjectDelete(obex_t *self, obex_object_t *object){ g_return_val_if_fail(object != NULL, -1); return obex_object_delete(object);}/** * OBEX_ObjectAddHeader - Attach a header to an object * @self: OBEX handle * @object: OBEX object * @hi: Header identifier * @hv: Header value * @hv_size: Header size * @flags: See obex.h for possible values * * Add a new header to an object. * * If you want all headers to fit in one packet, use the flag * %OBEX_FL_FIT_ONE_PACKET on all headers you add to an object. * * To stream a body add a body header with hv.bs = %NULL and set the flag * %OBEX_FL_STREAM_START. You will now get %OBEX_EV_STREAMEMPTY events as * soon as the the parser wants you to feed it with more data. * * When you get an %OBEX_EV_STREAMEMPTY event give the parser some data by * adding a body-header and set the flag %OBEX_EV_STREAM_DATA. When you * have no more data to send set the flag %OBEX_EV_STREAM_DATAEND instead. * * After adding a header you are free to do whatever you want with the buffer * if you are NOT streaming. If you are streaming you may not touch the * buffer until you get another %OBEX_EV_STREAMEMTPY or until the request * finishes. * * The headers will be sent in the order you add them. */gint OBEX_ObjectAddHeader(obex_t *self, obex_object_t *object, guint8 hi, obex_headerdata_t hv, guint32 hv_size, guint flags){ g_return_val_if_fail(object != NULL, -1); return obex_object_addheader(self, object, hi, hv, hv_size, flags);}/** * OBEX_ObjectGetNextHeader - Get next available header from an object * @self: OBEX handle * @object: OBEX object * @hi: Pointer to header identifier * @hv: Pointer to hv * @hv_size: Pointer to hv_size * * Returns 0 when no more headers are available. * * All headers are read-only. * * You will get the headers in the received order. */gint OBEX_ObjectGetNextHeader(obex_t *self, obex_object_t *object, guint8 *hi, obex_headerdata_t *hv, guint32 *hv_size){ g_return_val_if_fail(self != NULL, -1); g_return_val_if_fail(object != NULL, -1); return obex_object_getnextheader(self, object, hi, hv, hv_size);}/** * OBEX_ObjectReParseHeaders - Allow the user to parse again the rx headers * @self: OBEX handle * @object: OBEX object * * The user must have extracted all headers from the object before * calling this function (until %OBEX_ObjectGetNextHeader() returns 0). * Next call to %OBEX_ObjectGetNextHeader() will return the first received * header. * * Returns 1 on success * Returns 0 if failed due previous parsing not completed. */gint OBEX_ObjectReParseHeaders(obex_t *self, obex_object_t *object){ g_return_val_if_fail(self != NULL, -1); g_return_val_if_fail(object != NULL, -1); return obex_object_reparseheaders(self, object);}/** * OBEX_ObjectReadStream - Read data from body stream * @self: OBEX handle * @object: OBEX object * @buf: A pointer to a pointer which this function will set to a buffer which * shall be read (and ONLY read) after this function returns. * * To recieve the body as a stream call this function with buf = %NULL as soon * as you get an OBEX_EV_REQHINT event. * * You will now recieve %OBEX_EV_STREAMAVAIL events when data is available * for you. Call this function to get the data. * * Note! When receiving a stream data is not buffered so if you don't call this * function when you get an %OBEX_EV_STREAMAVAIL event data will be lost. * * Returns the number of bytes in buffer, or 0 for end-of-stream. */gint OBEX_ObjectReadStream(obex_t *self, obex_object_t *object, const guint8 **buf){ g_return_val_if_fail(self != NULL, -1); g_return_val_if_fail(object != NULL, -1); return obex_object_readstream(self, object, buf);}/** * OBEX_ObjectSetRsp - Sets the response to a received request. * @self: OBEX handle * @object: OBEX object * @rsp: Respose code in non-last packets * @lastrsp: Response code in last packet * * Returns -1 on error. */gint OBEX_ObjectSetRsp(obex_object_t *object, guint8 rsp, guint8 lastrsp){ g_return_val_if_fail(object != NULL, -1); return obex_object_setrsp(object, rsp, lastrsp);}/** * OBEX_ObjectGetNonHdrData - Get any data which was before headers * @object: OBEX object * @buffer: Pointer to a pointer which will point to a read-only buffer * * Returns the size of the buffer or -1 for error. */gint OBEX_ObjectGetNonHdrData(obex_object_t *object, guint8 **buffer){ g_return_val_if_fail(object != NULL, -1); if(!object->rx_nonhdr_data) return 0; *buffer = object->rx_nonhdr_data->data; return object->rx_nonhdr_data->len;}/** * OBEX_ObjectSetNonHdrData - Set data to send before headers * @object: OBEX object * @buffer: Data to send * @len: Length to data * * Some commands (notably SetPath) send data before headers. Use this * function to set this data. */gint OBEX_ObjectSetNonHdrData(obex_object_t *object, const guint8 *buffer, guint len){ //TODO: Check that we actually can send len bytes without violating MTU g_return_val_if_fail(object != NULL, -1); g_return_val_if_fail(buffer != NULL, -1); if(object->tx_nonhdr_data) return -1; object->tx_nonhdr_data = g_netbuf_new(len); if(object->tx_nonhdr_data == NULL) return -1; g_netbuf_put_data(object->tx_nonhdr_data, (guint8 *)buffer, len); return 1;}/** * OBEX_ObjectSetHdrOffset - Set headeroffset * @object: OBEX object * @offset: Desired offset * * Call this function when you get a OBEX_EV_REQHINT and you know that the * command has data before the headers comes. You do NOT need to use this * function on Connect and SetPath, they are handled automatically. */gint OBEX_ObjectSetHdrOffset(obex_object_t *object, guint offset){ g_return_val_if_fail(object != NULL, -1); object->headeroffset = offset; return 1;}/** * OBEX_UnicodeToChar - Simple unicode to char function. * @c: Destination (char) * @uc: Source (unicode) * @size: Length of destination buffer * * Buffers may overlap. Returns -1 on error. */gint OBEX_UnicodeToChar(guint8 *c, const guint8 *uc, gint size){ gint n; DEBUG(4, G_GNUC_FUNCTION "()\n"); g_return_val_if_fail(uc != NULL, -1); g_return_val_if_fail(c != NULL, -1); // Make sure buffer is big enough! for(n = 0; uc[n*2+1] != 0; n++); g_return_val_if_fail(n < size, -1); for(n = 0; uc[n*2+1] != 0; n++) c[n] = uc[n*2+1]; c[n] = 0; return 0;}/** * OBEX_CharToUnicode - Simple char to unicode function. * @uc: Destination (unicode) * @c: Source (char) * @size: Length of destination buffer * * Buffers may overlap. Returns -1 on error. */gint OBEX_CharToUnicode(guint8 *uc, const guint8 *c, gint size){ gint len, n; DEBUG(4, G_GNUC_FUNCTION "()\n"); g_return_val_if_fail(uc != NULL, -1); g_return_val_if_fail(c != NULL, -1); len = n = strlen(c); g_return_val_if_fail( (n*2 < size), -1); uc[n*2+1] = 0; uc[n*2] = 0; while(n--) { uc[n*2+1] = c[n]; uc[n*2] = 0; } return (len*2)+2 ;}/** * OBEX_GetResponseMessage - Return a human understandable string from a response-code. * @self: OBEX handle * @rsp: Response code. * * The returned GString shall be freed by you. Returns %NULL on error. */GString* OBEX_GetResponseMessage(obex_t *self, gint rsp){ DEBUG(4, G_GNUC_FUNCTION "()\n"); g_return_val_if_fail(self != NULL, NULL); return obex_get_response_message(self, rsp);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -