int OWNET_present(OWNET_HANDLE h, const char *onewire_path) { unsigned char buffer[MAX_READ_BUFFER_SIZE]; int return_value; struct request_packet s_request_packet; struct request_packet *rp = &s_request_packet; memset(rp, 0, sizeof(struct request_packet)); CONNIN_RLOCK; rp->owserver = find_connection_in(h); if (rp->owserver == NULL) { CONNIN_RUNLOCK; return -EBADF; } rp->path = (onewire_path == NULL) ? "/" : onewire_path; rp->read_value = buffer; rp->data_length = MAX_READ_BUFFER_SIZE; rp->data_offset = 0; return_value = ServerPresence(rp); /* 0 is ok, <0 not found */ CONNIN_RUNLOCK; return return_value; }
// Look on a given connection for the device static INDEX_OR_ERROR CheckThisConnection(int bus_nr, struct parsedname *pn) { struct parsedname s_pn_copy; struct parsedname * pn_copy = &s_pn_copy ; struct connection_in * in = find_connection_in(bus_nr) ; INDEX_OR_ERROR connection_result = INDEX_BAD ; if ( in == NO_CONNECTION ) { return INDEX_BAD ; } memcpy(pn_copy, pn, sizeof(struct parsedname)); // shallow copy pn_copy->selected_connection = in; if ( BAD( TestConnection(pn_copy) ) ) { // Connection currently disconnected return INDEX_BAD; } else if (BusIsServer(in)) { // Server if ( INDEX_VALID( ServerPresence(pn_copy) ) ) { connection_result = in->index; } } else if ( in->iroutines.flags & ADAP_FLAG_sham ) { return INDEX_BAD ; } else if ( in->iroutines.flags & ADAP_FLAG_presence_from_dirblob ) { // local connection with a dirblob (like fake, mock, ...) if ( GOOD( PresenceFromDirblob( pn_copy ) ) ) { connection_result = in->index ; } } else { // local connection but need to ask directly struct transaction_log t[] = { TRXN_NVERIFY, TRXN_END, }; if ( GOOD( BUS_transaction(t, pn_copy) ) ) { connection_result = in->index ; } } if ( connection_result == INDEX_BAD ) { LEVEL_DEBUG("Presence of "SNformat" NOT found on bus %s",SNvar(pn_copy->sn),SAFESTRING(DEVICENAME(in))) ; } else { LEVEL_DEBUG("Presence of "SNformat" FOUND on bus %s",SNvar(pn_copy->sn),SAFESTRING(DEVICENAME(in))) ; Cache_Add_Device(in->index,pn_copy->sn) ; // add or update cache */ } return connection_result ; }
int OWNET_lread(OWNET_HANDLE h, const char *onewire_path, char *return_string, size_t size, off_t offset) { struct request_packet s_request_packet; struct request_packet *rp = &s_request_packet; int return_value; memset(rp, 0, sizeof(struct request_packet)); CONNIN_RLOCK; rp->owserver = find_connection_in(h); if (rp->owserver == NULL) { CONNIN_RUNLOCK; return -EBADF; } rp->path = (onewire_path == NULL) ? "/" : onewire_path; rp->read_value = (unsigned char *)return_string; rp->data_length = size; rp->data_offset = offset; return_value = ServerRead(rp); CONNIN_RUNLOCK; return return_value; }
int OWNET_read(OWNET_HANDLE h, const char *onewire_path, char **return_string) { unsigned char buffer[MAX_READ_BUFFER_SIZE]; int return_value; struct request_packet s_request_packet; struct request_packet *rp = &s_request_packet; memset(rp, 0, sizeof(struct request_packet)); CONNIN_RLOCK; rp->owserver = find_connection_in(h); if (rp->owserver == NULL) { CONNIN_RUNLOCK; return -EBADF; } rp->path = (onewire_path == NULL) ? "/" : onewire_path; rp->read_value = buffer; rp->data_length = MAX_READ_BUFFER_SIZE; rp->data_offset = 0; /* Fix from nleonard671 to add a terminating NULL */ return_value = ServerRead(rp); if (return_value > 0) { *return_string = malloc(return_value+1); if (*return_string == NULL) { return_value = -ENOMEM; } else { memcpy(*return_string, buffer, return_value); (*return_string)[return_value] = '\0' ; } } CONNIN_RUNLOCK; return return_value; }
int OWNET_put(OWNET_HANDLE h, const char *onewire_path, const char *value_string, size_t size) { struct request_packet s_request_packet; struct request_packet *rp = &s_request_packet; int return_value; memset(rp, 0, sizeof(struct request_packet)); CONNIN_RLOCK; rp->owserver = find_connection_in(h); if (rp->owserver == NULL) { CONNIN_RUNLOCK; return -EBADF; } rp->path = (onewire_path == NULL) ? "/" : onewire_path; rp->write_value = (const unsigned char *) value_string; rp->data_length = size; rp->data_offset = 0; return_value = ServerWrite(rp); CONNIN_RUNLOCK; return return_value; }