static int create_handover_agent(const char *sender, const char *path, enum ho_agent_carrier carrier) { struct near_handover_agent *agent; agent = g_try_malloc0(sizeof(struct near_handover_agent)); if (!agent) return -ENOMEM; agent->sender = g_strdup(sender); agent->path = g_strdup(path); agent->carrier = carrier; agent->watch = g_dbus_add_disconnect_watch(connection, sender, handover_agent_disconnect, agent, NULL); g_hash_table_insert(ho_agent_hash, GINT_TO_POINTER(carrier), agent); DBG("handover agent registered"); switch (agent->carrier) { case HO_AGENT_BT: /* stop watching for legacy bluez */ __near_bluetooth_legacy_stop(); break; case HO_AGENT_WIFI: case HO_AGENT_UNKNOWN: break; } return 0; }
/** * Convert a numeric frequency value to the "natural" string representation * of its period. * * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms". * * @param frequency The frequency in Hz. * * @return A g_try_malloc()ed string representation of the frequency value, * or NULL upon errors. The caller is responsible to g_free() the * memory. */ SR_API char *sr_period_string(uint64_t frequency) { char *o; int r; /* Allocate enough for a uint64_t as string + " ms". */ if (!(o = g_try_malloc0(30 + 1))) { sr_err("%s: o malloc failed", __func__); return NULL; } if (frequency >= SR_GHZ(1)) r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000); else if (frequency >= SR_MHZ(1)) r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000); else if (frequency >= SR_KHZ(1)) r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000); else r = snprintf(o, 30, "%" PRIu64 " s", frequency); if (r < 0) { /* Something went wrong... */ g_free(o); return NULL; } return o; }
/** * Convert a numeric voltage value to the "natural" string representation * of its voltage value. The voltage is specified as a rational number's * numerator and denominator. * * E.g. a value of 300000 would be converted to "300mV", 2 to "2V". * * @param v_p The voltage numerator. * @param v_q The voltage denominator. * * @return A g_try_malloc()ed string representation of the voltage value, * or NULL upon errors. The caller is responsible to g_free() the * memory. */ SR_API char *sr_voltage_string(uint64_t v_p, uint64_t v_q) { int r; char *o; if (!(o = g_try_malloc0(30 + 1))) { sr_err("%s: o malloc failed", __func__); return NULL; } if (v_q == 1000) r = snprintf(o, 30, "%" PRIu64 "mV", v_p); else if (v_q == 1) r = snprintf(o, 30, "%" PRIu64 "V", v_p); else r = snprintf(o, 30, "%gV", (float)v_p / (float)v_q); if (r < 0) { /* Something went wrong... */ g_free(o); return NULL; } return o; }
static GAtPPP *ppp_init_common(GAtHDLC *hdlc, gboolean is_server, guint32 ip) { GAtPPP *ppp; ppp = g_try_malloc0(sizeof(GAtPPP)); if (ppp == NULL) return NULL; ppp->hdlc = g_at_hdlc_ref(hdlc); ppp->ref_count = 1; /* set options to defaults */ ppp->mru = DEFAULT_MRU; ppp->mtu = DEFAULT_MTU; /* initialize the lcp state */ ppp->lcp = lcp_new(ppp, is_server); /* initialize IPCP state */ ppp->ipcp = ipcp_new(ppp, is_server, ip); g_at_hdlc_set_receive(ppp->hdlc, ppp_receive, ppp); g_at_io_set_disconnect_function(g_at_hdlc_get_io(ppp->hdlc), io_disconnect, ppp); if (is_server) ppp_enter_phase(ppp, PPP_PHASE_ESTABLISHMENT); return ppp; }
static void extract_addr(DBusMessageIter *value, struct supplicant_result *result) { DBusMessageIter array; struct ether_addr eth; unsigned char *addr; int addr_len; dbus_message_iter_recurse(value, &array); dbus_message_iter_get_fixed_array(&array, &addr, &addr_len); if (addr_len != 6) return; result->addr = g_try_malloc(addr_len); if (result->addr == NULL) return; memcpy(result->addr, addr, addr_len); result->addr_len = addr_len; result->path = g_try_malloc0(13); if (result->path == NULL) return; memcpy(ð, addr, sizeof(eth)); snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x", eth.ether_addr_octet[0], eth.ether_addr_octet[1], eth.ether_addr_octet[2], eth.ether_addr_octet[3], eth.ether_addr_octet[4], eth.ether_addr_octet[5]); }
SR_PRIV int event(struct sr_output *o, int event_type, uint8_t **data_out, uint64_t *length_out) { struct context *ctx; int outsize; uint8_t *outbuf; ctx = o->internal; switch (event_type) { case SR_DF_TRIGGER: ctx->mark_trigger = ctx->spl_cnt; *data_out = NULL; *length_out = 0; break; case SR_DF_END: outsize = ctx->num_enabled_probes * (ctx->samples_per_line + 20) + 512; if (!(outbuf = g_try_malloc0(outsize))) { sr_err("text out: %s: outbuf malloc failed", __func__); return SR_ERR_MALLOC; } flush_linebufs(ctx, outbuf); *data_out = outbuf; *length_out = strlen((const char *)outbuf); g_free(o->internal); o->internal = NULL; break; default: *data_out = NULL; *length_out = 0; break; } return SR_OK; }
/***************************************************************************** * neardal_handoveragent_prv_manage: create or release an agent and register * or unregister it with neardal object manager and Neard ****************************************************************************/ errorCode_t neardal_handoveragent_prv_manage( neardal_handover_agent_t agentData) { errorCode_t err = NEARDAL_SUCCESS; neardalObjectSkeleton *objSkel; neardalHandoverAgent *handoverAgent; neardal_handover_agent_t *data; NEARDAL_TRACEIN(); if (agentData.cb_oob_push_agent != NULL && agentData.cb_oob_req_agent != NULL) { data = g_try_malloc0(sizeof(neardal_handover_agent_t)); if (data == NULL) return NEARDAL_ERROR_NO_MEMORY; memcpy(data, &agentData, sizeof(neardal_handover_agent_t)); data->objPath = g_strdup(agentData.objPath); data->carrierType = g_strdup(agentData.carrierType); NEARDAL_TRACEF("Create agent '%s'\n", data->objPath); objSkel = neardal_object_skeleton_new (data->objPath); handoverAgent = neardal_handover_agent_skeleton_new(); neardal_object_skeleton_set_handover_agent(objSkel, handoverAgent); /* Handle RequestOOB() D-Bus method invocations */ g_signal_connect( handoverAgent, "handle-request-oob" , G_CALLBACK (on_RequestOOB) , data); /* Handle PushOOB() D-Bus method invocations */ g_signal_connect( handoverAgent, "handle-push-oob" , G_CALLBACK (on_PushOOB) , data); /* Handle Release D-Bus method invocations */ g_signal_connect( handoverAgent, "handle-release" , G_CALLBACK (on_Handover_Release), data); g_signal_connect( neardalMgr.agentMgr, "object-removed" , G_CALLBACK (on_handover_object_removed) , data); g_object_unref(handoverAgent); /* Export the object */ g_dbus_object_manager_server_export(neardalMgr.agentMgr , G_DBUS_OBJECT_SKELETON (objSkel)); g_object_unref (objSkel); } else { NEARDAL_TRACEF("Release agent '%s'\n", agentData.objPath); if (neardal_agent_prv_remove(agentData.objPath) == TRUE) err = NEARDAL_SUCCESS; else err = NEARDAL_ERROR_DBUS; } return err; }
static bool plugin_mapping_new(zathura_plugin_manager_t* plugin_manager, const gchar* type, zathura_plugin_t* plugin) { g_return_val_if_fail(plugin_manager != NULL, false); g_return_val_if_fail(type != NULL, false); g_return_val_if_fail(plugin != NULL, false); GIRARA_LIST_FOREACH(plugin_manager->type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping) if (g_content_type_equals(type, mapping->type)) { girara_list_iterator_free(iter); return false; } GIRARA_LIST_FOREACH_END(plugin_manager->type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping); zathura_type_plugin_mapping_t* mapping = g_try_malloc0(sizeof(zathura_type_plugin_mapping_t)); if (mapping == NULL) { return false; } mapping->type = g_strdup(type); mapping->plugin = plugin; girara_list_append(plugin_manager->type_plugin_mapping, mapping); return true; }
mc_config_t * mc_config_init (const gchar * ini_path) { mc_config_t *mc_config; struct stat st; mc_config = g_try_malloc0 (sizeof (mc_config_t)); if (mc_config == NULL) return NULL; mc_config->handle = g_key_file_new (); if (mc_config->handle == NULL) { g_free (mc_config); return NULL; } if (!ini_path || !exist_file (ini_path)) { return mc_config; } if (!mc_stat (ini_path, &st) && st.st_size) { /* file present and not empty */ g_key_file_load_from_file (mc_config->handle, ini_path, G_KEY_FILE_KEEP_COMMENTS, NULL); } mc_config->ini_path = g_strdup (ini_path); return mc_config; }
static struct attr_handle_s * _alloc_attr_handle(const gchar * chunk_path) { struct attr_handle_s *attr_handle = NULL; attr_handle = g_try_malloc0(sizeof(struct attr_handle_s)); if (!attr_handle) goto error_handle; attr_handle->chunk_path = g_strdup(chunk_path); if (!attr_handle->chunk_path) goto error_chunk_path; attr_handle->attr_path = g_strdup_printf("%s.attr", chunk_path); if (!attr_handle->attr_path) goto error_attr_path; attr_handle->attr_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); if (!attr_handle->attr_hash) goto error_hash; attr_handle->attr_file_des = -1; attr_handle->chunk_file_des = -1; return attr_handle; error_hash: g_free(attr_handle->attr_path); error_attr_path: g_free(attr_handle->chunk_path); error_chunk_path: g_free(attr_handle); error_handle: return NULL; }
struct http_put_dest_s * http_put_add_dest(struct http_put_s *p, const char *url, gpointer u) { g_assert(p != NULL); g_assert(p->state == HTTP_WHOLE_BEGIN); g_assert(url != NULL); g_assert(u != NULL); struct http_put_dest_s *dest = g_try_malloc0(sizeof(struct http_put_dest_s)); dest->http_put = p; dest->url = g_strdup(url); dest->handle = NULL; dest->user_data = u; dest->headers = NULL; dest->curl_headers = NULL; dest->response_headers = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); dest->bytes_sent = 0; dest->http_code = 0; dest->state = HTTP_SINGLE_BEGIN; p->dests = g_slist_append(p->dests, dest); return dest; }
static int init(struct sr_output *o) { struct context *ctx; struct sr_probe *probe; GSList *l; sr_spew("Initializing output module."); if (!o || !o->sdi) return SR_ERR_ARG; if (!(ctx = g_try_malloc0(sizeof(struct context)))) { sr_err("Output module context malloc failed."); return SR_ERR_MALLOC; } o->internal = ctx; /* Get the number of probes and their names. */ ctx->probelist = g_ptr_array_new(); for (l = o->sdi->probes; l; l = l->next) { probe = l->data; if (!probe || !probe->enabled) continue; g_ptr_array_add(ctx->probelist, probe->name); ctx->num_enabled_probes++; } return SR_OK; }
/** * Add a datafeed callback to the current session. * * @param cb Function to call when a chunk of data is received. * Must not be NULL. * @param cb_data Opaque pointer passed in by the caller. * * @return SR_OK upon success, SR_ERR_BUG if no session exists. */ SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb, void *cb_data) { struct datafeed_callback *cb_struct; if (!session) { sr_err("%s: session was NULL", __func__); return SR_ERR_BUG; } if (!cb) { sr_err("%s: cb was NULL", __func__); return SR_ERR_ARG; } if (!(cb_struct = g_try_malloc0(sizeof(struct datafeed_callback)))) return SR_ERR_MALLOC; cb_struct->cb = cb; cb_struct->cb_data = cb_data; session->datafeed_callbacks = g_slist_append(session->datafeed_callbacks, cb_struct); return SR_OK; }
/** * Allocate memory for image display view structure and initialize it with * correct values. * * \return pointer to image display specific data */ ViewImageDisplay* view_idv_create(ViewGeneric *parent) { const ConfigStorage* conf; ViewImageDisplay *result = NULL; ASSERT( NULL != parent ); if ( NULL == ( result = (ViewImageDisplay*) g_try_malloc0(sizeof(ViewImageDisplay))) ) { ERROR_SET(ERROR.MEMORY_FAILURE); return result; } result->dimension.x = 0; result->dimension.y = 0; result->view_buffer = NULL; // either get parameters for zooming from configuration or get default if it // is not present conf = config_get(); if ( NULL == conf ) { ERROR_LOG(); result->zoom_factor = 1.0; result->zoom_type = ZOOM_FIT_CUSTOM; result->pan_percentage = VIEW_PAN_PERCENTAGE; } else { result->zoom_factor = conf->zoom; result->zoom_type = conf->auto_zoom; result->pan_percentage = conf->pan_percentage; } return result; }
void *parcel_r_raw(struct parcel *p, int *len) { char *ret; *len = parcel_r_int32(p); if (p->malformed || *len <= 0) return NULL; if (p->offset + *len > p->size) { ofono_error("%s: parcel is too small", __func__); p->malformed = 1; return NULL; } ret = g_try_malloc0(*len); if (ret == NULL) { ofono_error("%s: out of memory (%d bytes)", __func__, *len); return NULL; } memcpy(ret, p->data + p->offset, *len); p->offset += *len; return ret; }
/* This function reads the MAD2 sector */ static int mifare_read_MAD2(void *data) { struct mifare_cookie *mf_ck = data; int err = 0; DBG(""); /* As auth is ok, allocate Mifare Access Directory v1 */ mf_ck->mad_2 = g_try_malloc0(STD_SECTOR_SIZE); if (!mf_ck->mad_2) { near_error("Memory allocation failed (MAD2)"); err = -ENOMEM; goto out_err; } err = mifare_read_sector(data, (uint8_t *) mf_ck->mad_2, (int) STD_SECTOR_SIZE, MAD2_SECTOR, /* sector 0x10 */ WITH_TRAILER, /* Want Trailer */ read_MAD2_complete); if (err < 0) goto out_err; return err; out_err: return mifare_release(err, mf_ck); }
static int grant_client_access (struct rdma_cm_id *client, void *mem, size_t mem_size, guint type) { struct kiro_connection_context *ctx = (struct kiro_connection_context *) (client->context); ctx->rdma_mr = (struct kiro_rdma_mem *)g_try_malloc0 (sizeof (struct kiro_rdma_mem)); if (!ctx->rdma_mr) { g_critical ("Failed to allocate RDMA Memory Container: %s", strerror (errno)); return -1; } ctx->rdma_mr->mem = mem; ctx->rdma_mr->size = mem_size; ctx->rdma_mr->mr = rdma_reg_read (client, ctx->rdma_mr->mem, ctx->rdma_mr->size); if (!ctx->rdma_mr->mr) { g_critical ("Failed to register RDMA Memory Region: %s", strerror (errno)); kiro_destroy_rdma_memory (ctx->rdma_mr); return -1; } struct kiro_ctrl_msg *msg = (struct kiro_ctrl_msg *) (ctx->cf_mr_send->mem); msg->msg_type = type; msg->peer_mri = * (ctx->rdma_mr->mr); if (!send_msg (client, ctx->cf_mr_send)) { g_warning ("Failure while trying to post SEND: %s", strerror (errno)); kiro_destroy_rdma_memory (ctx->rdma_mr); return -1; } g_debug ("RDMA MRI sent to client"); return 0; }
static int init_known_adapters(int ctl) { struct hci_dev_list_req *dl; struct hci_dev_req *dr; int i, err; dl = g_try_malloc0(HCI_MAX_DEV * sizeof(struct hci_dev_req) + sizeof(uint16_t)); if (!dl) { err = errno; error("Can't allocate devlist buffer: %s (%d)", strerror(err), err); return -err; } dl->dev_num = HCI_MAX_DEV; dr = dl->dev_req; if (ioctl(ctl, HCIGETDEVLIST, (void *) dl) < 0) { err = errno; error("Can't get device list: %s (%d)", strerror(err), err); g_free(dl); return -err; } for (i = 0; i < dl->dev_num; i++, dr++) { device_event(HCI_DEV_REG, dr->dev_id); if (hci_test_bit(HCI_UP, &dr->dev_opt)) device_event(HCI_DEV_UP, dr->dev_id); } g_free(dl); return 0; }
/***************************************************************************** * neardal_tag_prv_add: add new NFC tag, initialize DBus Proxy connection, * register tag signal ****************************************************************************/ errorCode_t neardal_tag_prv_add(gchar *tagName, void *parent) { errorCode_t err = NEARDAL_ERROR_NO_MEMORY; TagProp *tagProp = NULL; AdpProp *adpProp = parent; NEARDAL_ASSERT_RET((tagName != NULL) && (parent != NULL) , NEARDAL_ERROR_INVALID_PARAMETER); NEARDAL_TRACEF("Adding tag:%s\n", tagName); tagProp = g_try_malloc0(sizeof(TagProp)); if (tagProp == NULL) goto error; tagProp->name = g_strdup(tagName); tagProp->parent = adpProp; adpProp->tagList = g_list_prepend(adpProp->tagList, tagProp); err = neardal_tag_prv_init(tagProp); NEARDAL_TRACEF("NEARDAL LIB tagList contains %d elements\n", g_list_length(adpProp->tagList)); return err; error: if (tagProp->name != NULL) g_free(tagProp->name); if (tagProp != NULL) g_free(tagProp); return err; }
static int hw_init(const char *devinfo) { struct sr_dev_inst *sdi; struct context *ctx; int devcnt = 0; /* Avoid compiler warnings. */ (void)devinfo; if (libusb_init(&genericdmm_usb_context) != 0) { sr_err("genericdmm: Failed to initialize USB."); return 0; } if (!(ctx = g_try_malloc0(sizeof(struct context)))) { sr_err("genericdmm: ctx malloc failed."); return 0; } devcnt = g_slist_length(genericdmm_dev_insts); if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE, "Generic DMM", NULL, NULL))) { sr_err("genericdmm: sr_dev_inst_new returned NULL."); return 0; } sdi->priv = ctx; genericdmm_dev_insts = g_slist_append(genericdmm_dev_insts, sdi); /* Always initialized just one device instance. */ return 0; }
static int create_search_context(struct search_context **ctxt, const bdaddr_t *src, const bdaddr_t *dst, uuid_t *uuid) { sdp_session_t *s; GIOChannel *chan; if (!ctxt) return -EINVAL; s = get_sdp_session(src, dst); if (!s) return -errno; *ctxt = g_try_malloc0(sizeof(struct search_context)); if (!*ctxt) { sdp_close(s); return -ENOMEM; } bacpy(&(*ctxt)->src, src); bacpy(&(*ctxt)->dst, dst); (*ctxt)->session = s; (*ctxt)->uuid = *uuid; chan = g_io_channel_unix_new(sdp_get_socket(s)); (*ctxt)->io_id = g_io_add_watch(chan, G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL, connect_watch, *ctxt); g_io_channel_unref(chan); return 0; }
/***************************************************************************** * neardal_dev_prv_add: add new NFC device, initialize DBus Proxy connection, * register dev signal ****************************************************************************/ errorCode_t neardal_dev_prv_add(gchar *devName, void *parent) { errorCode_t err = NEARDAL_ERROR_NO_MEMORY; DevProp *devProp = NULL; AdpProp *adpProp = parent; NEARDAL_ASSERT_RET( ((adpProp != NULL) && (devName != NULL)) , NEARDAL_ERROR_INVALID_PARAMETER); NEARDAL_TRACEF("Adding dev:%s\n", devName); devProp = g_try_malloc0(sizeof(DevProp)); if (devProp == NULL) goto error; devProp->name = g_strdup(devName); devProp->parent = adpProp; adpProp->devList = g_list_prepend(adpProp->devList, devProp); NEARDAL_TRACEF("NEARDAL LIB devList contains %d elements\n", g_list_length(adpProp->devList)); return NEARDAL_SUCCESS; error: if (devProp->name != NULL) g_free(devProp->name); if (devProp != NULL) g_free(devProp); return err; }
static void extract_ssid(DBusMessageIter *value, struct supplicant_result *result) { DBusMessageIter array; unsigned char *ssid; int ssid_len, i; dbus_message_iter_recurse(value, &array); dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len); if (ssid_len < 1) return; if (ssid[0] == '\0') return; result->ssid = g_try_malloc(ssid_len); if (result->ssid == NULL) return; memcpy(result->ssid, ssid, ssid_len); result->ssid_len = ssid_len; result->name = g_try_malloc0(ssid_len + 1); if (result->name == NULL) return; for (i = 0; i < ssid_len; i++) { if (g_ascii_isprint(ssid[i])) result->name[i] = ssid[i]; else result->name[i] = ' '; } }
/** * Run user selection loop. Asks for usename and if its longer than a character (newline marker) * that user is attempted to be loaded. If failed, username is asked again. */ void run_user_loop() { gboolean loop_user = TRUE; // Allocate buffer gchar* user = g_try_malloc0(USERNAME_MAX_CHAR); // Selection loop while(loop_user) { g_print("Give username whose tests to run (max. %d chars) or quit (q): ",USERNAME_MAX_CHAR-1); // Get until newline user = fgets(user,USERNAME_MAX_CHAR,stdin); // Replace newline if(user[strlen(user)-1] == '\n') user[strlen(user)-1]= '\0'; // Contains only 1 character or less if(strlen(user) < 1) { g_print("Empty username given.\n"); memset(user,'\0',USERNAME_MAX_CHAR); } // Quitting else if (g_strcmp0("quit",user) == 0 || g_strcmp0("q",user) == 0) { g_free(user); return; } else { if(run_test_selection(user)) loop_user = FALSE; } } g_free(user); }
char* girara_get_home_directory(const char* user) { if (user == NULL || g_strcmp0(user, g_get_user_name()) == 0) { return g_strdup(g_get_home_dir()); } // XXX: The following code is very unportable. struct passwd pwd; struct passwd* result = NULL; #ifdef _SC_GETPW_R_SIZE_MAX int bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); if (bufsize < 0) { bufsize = 4096; } #else int bufsize = 4096; #endif char* buffer = g_try_malloc0(sizeof(char) * bufsize); if (buffer == NULL) { return NULL; } getpwnam_r(user, &pwd, buffer, bufsize, &result); if (result == NULL) { g_free(buffer); return NULL; } char* dir = g_strdup(pwd.pw_dir); g_free(buffer); return dir; }
static struct wifi_sc *decode_from_tlv(const uint8_t *tlv_msg, int length) { struct wifi_sc *wsc; uint16_t attr, len; uint8_t *bt; int pos; DEid id; if (tlv_msg == NULL || length == 0) return NULL; wsc = g_try_malloc0(sizeof(struct wifi_sc)); if (wsc == NULL) return NULL; pos = 0; while (pos < length) { pos += get_2b_from_tlv(tlv_msg+pos, &attr); pos += get_2b_from_tlv(tlv_msg+pos, &len); if (len > length - pos) goto error; id = get_de_id(attr); if (id == DE_MAX) { pos += len; continue; } if (is_de_length_fine(id, len) == FALSE) goto error; switch (id) { case DE_AUTHENTICATION_TYPE: break; case DE_NETWORK_KEY: wsc->passphrase = get_string_from_tlv(tlv_msg+pos, len); break; case DE_SSID: bt = get_byte_array_from_tlv(tlv_msg+pos, len); wsc->ssid = get_hexstr_from_byte_array(bt, len); g_free(bt); break; case DE_MAX: /* Falling back. Should never happen though. */ default: break; } pos += len; } return wsc; error: free_wifi_sc(wsc); return NULL; }
/** * Convert a numeric time(ns) value to the "natural" string representation * of its period. * * E.g. a value of 3000000 would be converted to "3 ms", 20000 to "20 us". * * @param time The time in ns. * * @return A g_try_malloc()ed string representation of the time value, * or NULL upon errors. The caller is responsible to g_free() the * memory. */ SR_API char *sr_time_string(uint64_t time) { char *o; int r; /* Allocate enough for a uint64_t as string + " ms". */ if (!(o = g_try_malloc0(30 + 1))) { sr_err("%s: o malloc failed", __func__); return NULL; } if (time >= 1000000000) r = snprintf(o, 30, "%" PRIu64 " s", time / 1000000000); else if (time >= 1000000) r = snprintf(o, 30, "%" PRIu64 " ms", time / 1000000); else if (time >= 1000) r = snprintf(o, 30, "%" PRIu64 " us", time / 1000); else r = snprintf(o, 30, "%" PRIu64 " ns", time); if (r < 0) { /* Something went wrong... */ g_free(o); return NULL; } return o; }
static guint bluetooth_connect(const char *source, const char *destination, const char *service, uint16_t port, obc_transport_func func, void *user_data) { struct bluetooth_session *session; static guint id = 0; DBG("src %s dest %s service %s port %u", source, destination, service, port); if (destination == NULL) return 0; session = g_try_malloc0(sizeof(*session)); if (session == NULL) return 0; session->id = ++id; session->func = func; session->port = port; session->user_data = user_data; str2ba(destination, &session->dst); str2ba(source, &session->src); if (session_connect(session) < 0) { g_free(session); return 0; } session->service = g_strdup(service); sessions = g_slist_prepend(sessions, session); return session->id; }
static void handle_uhid_output(struct hid_device *dev, struct uhid_output_req *output) { int fd, i; uint8_t *req = NULL; uint8_t req_size = 0; if (!(dev->ctrl_io)) return; req_size = 1 + (output->size / 2); req = g_try_malloc0(req_size); if (!req) return; req[0] = HID_MSG_SET_REPORT | output->rtype; for (i = 0; i < (req_size - 1); i++) sscanf((char *) &(output->data)[i * 2], "%hhx", &req[1 + i]); fd = g_io_channel_unix_get_fd(dev->ctrl_io); if (write(fd, req, req_size) < 0) error("error writing set_report: %s (%d)", strerror(errno), errno); g_free(req); }
static int ndef_register(const char *sender, const char *path, const char *record_type) { struct near_ndef_agent *agent; DBG("%s registers path %s for %s", sender, path, record_type); if (g_hash_table_lookup(ndef_app_hash, record_type)) return -EEXIST; agent = g_try_malloc0(sizeof(struct near_ndef_agent)); if (!agent) return -ENOMEM; agent->sender = g_strdup(sender); agent->path = g_strdup(path); agent->record_type = g_strdup(record_type); if (!agent->sender || !agent->path || !agent->record_type) { g_free(agent); return -ENOMEM; } agent->watch = g_dbus_add_disconnect_watch(connection, sender, ndef_agent_disconnect, agent, NULL); g_hash_table_insert(ndef_app_hash, agent->record_type, agent); return 0; }