// This function should return number of bytes read... not status. static SIZE_OR_ERROR FS_r_given_bus(struct one_wire_query *owq) { // Device locking occurs here struct parsedname *pn = PN(owq); SIZE_OR_ERROR read_or_error = 0; LEVEL_DEBUG("About to read <%s> extension=%d size=%d offset=%d",SAFESTRING(pn->path),(int) pn->extension,(int) OWQ_size(owq),(int) OWQ_offset(owq)); if (KnownBus(pn) && BusIsServer(pn->selected_connection)) { /* The bus is not local... use a network connection instead */ LEVEL_DEBUG("pid=%ld call ServerRead", pthread_self()); // Read afar -- returns already formatted in buffer read_or_error = ServerRead(owq); LEVEL_DEBUG("back from server"); //printf("FS_r_given_bus pid=%ld r=%d\n",pthread_self(), read_or_error); } else { STAT_ADD1(read_calls); /* statistics */ if (DeviceLockGet(pn) == 0) { read_or_error = FS_r_local(owq); // this returns status DeviceLockRelease(pn); LEVEL_DEBUG("return=%d", read_or_error); if (read_or_error >= 0) { // local success -- now format in buffer read_or_error = OWQ_parse_output(owq); // this returns nr. bytes } } else { LEVEL_DEBUG("Cannot lock bus to perform read") ; read_or_error = -EADDRINUSE; } } LEVEL_DEBUG("After read is performed (bytes or error %d)", read_or_error); Debug_OWQ(owq); return read_or_error; }
// This function should return number of bytes read... not status. // Works for all the virtual directories, like statistics, interface, ... // Doesn't need three-peat and bus was already set or not needed. static SIZE_OR_ERROR FS_r_virtual(struct one_wire_query *owq) { struct parsedname *pn = PN(owq); SIZE_OR_ERROR read_status = 0; if (SpecifiedRemoteBus(pn)) { /* The bus is not local... use a network connection instead */ // Read afar -- returns already formatted in buffer read_status = ServerRead(owq); LEVEL_DEBUG("back from server"); Debug_OWQ(owq); } else { /* local bus -- any special locking needs? */ STAT_ADD1(read_calls); /* statistics */ switch (pn->type) { case ePN_structure: read_status = FS_structure(owq); break; case ePN_interface: BUSLOCK(pn); read_status = FS_r_local(owq); // this returns status BUSUNLOCK(pn); break; case ePN_statistics: // reading /statistics/read/tries.ALL // will cause a deadlock since it calls STAT_ADD1(read_array); // Should perhaps create a new mutex for this. // Comment out this STATLOCK until it's solved. // STATLOCK now done at time of actual read in ow_stats.c read_status = FS_r_local(owq); // this returns status break; default: read_status = FS_r_local(owq); // this returns status } if (read_status >= 0) { // local success -- now format in buffer read_status = OWQ_parse_output(owq); // this returns nr. bytes } } LEVEL_DEBUG("return %d", read_status); return read_status; }
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; }