/**************************************************************************** ** ** ** Name: network_api_read_data() ** ** ** ** Description: Read data received from the network sublayer ** ** ** ** Inputs: fd: File descriptor of the connection endpoint ** ** from which data have been received ** ** Others: _network_api_id ** ** ** ** Outputs: Return: The number of bytes read when success; ** ** RETURNerror otherwise ** ** Others: _network_api_recv_buffer, _network_api_id ** ** ** ***************************************************************************/ int network_api_read_data(int fd) { LOG_FUNC_IN; int rbytes; /* Sanity check */ int sfd = network_api_get_fd(); if (fd != sfd) { LOG_TRACE(ERROR, "NET-API - Endpoint %d is not the one created for communication with the network sublayer (%d)", fd, sfd); LOG_FUNC_RETURN (RETURNerror); } memset(_network_api_recv_buffer, 0, NETWORK_API_RECV_BUFFER_SIZE); /* Receive data from the network sublayer */ rbytes = NETWORK_API_RECV(_network_api_recv_buffer, NETWORK_API_RECV_BUFFER_SIZE); if (rbytes == RETURNerror) { LOG_TRACE(ERROR, "NET-API - recv() failed, %s", strerror(errno)); LOG_FUNC_RETURN (RETURNerror); } else if (rbytes == 0) { LOG_TRACE(WARNING, "NET-API - A signal was caught"); } else { LOG_TRACE(INFO, "NET-API - %d bytes received from the network " "sublayer", rbytes); LOG_DUMP(_network_api_recv_buffer, rbytes); } LOG_FUNC_RETURN (rbytes); }
/**************************************************************************** ** ** ** Name: network_api_send_data() ** ** ** ** Description: Send data to the network sublayer ** ** ** ** Inputs: fd: File descriptor of the connection endpoint ** ** to which data have to be sent ** ** length: Number of bytes to send ** ** Others: _network_api_send_buffer, _network_api_id ** ** ** ** Outputs: Return: The number of bytes sent when success; ** ** RETURNerror otherwise ** ** Others: None ** ** ** ***************************************************************************/ int network_api_send_data(int fd, int length) { LOG_FUNC_IN; int sbytes; /* Sanity check */ int sfd = network_api_get_fd(); if (fd != sfd) { LOG_TRACE(ERROR, "NET-API - Endpoint %d is not the one created for communication with the network sublayer (%d)", fd, sfd); LOG_FUNC_RETURN (RETURNerror); } /* Send data to the network sublayer */ sbytes = NETWORK_API_SEND(_network_api_send_buffer, length); if (sbytes == RETURNerror) { LOG_TRACE(ERROR, "NET-API - send() failed, %s", strerror(errno)); LOG_FUNC_RETURN (RETURNerror); } else if (sbytes == 0) { LOG_TRACE(WARNING, "NET-API - A signal was caught"); } else { LOG_TRACE(INFO, "NET-API - %d bytes sent to the network sublayer", sbytes); LOG_DUMP(_network_api_send_buffer, sbytes); } LOG_FUNC_RETURN (sbytes); }
void sid_free(struct sid_pool* pool, sid_t sid) { #ifdef DEBUG_SID LOG_DUMP("SID_FREE: %d", (int) sid); #endif pool->map[sid] = 0; pool->count--; }
void sid_pool_destroy(struct sid_pool* pool) { #ifdef DEBUG_SID LOG_DUMP("SID_POOL: destroying, current allocs=%d", (int) pool->count); #endif hub_free(pool->map); hub_free(pool); }
static int config_parse_line(char* line, int line_count, void* ptr_data) { char* pos; char* key; char* data; struct hub_config* config = (struct hub_config*) ptr_data; strip_off_ini_line_comments(line, line_count); if (!*line) return 0; LOG_DUMP("config_parse_line(): '%s'", line); if (!is_valid_utf8(line)) { LOG_WARN("Invalid utf-8 characters on line %d", line_count); } if ((pos = strchr(line, '=')) != NULL) { pos[0] = 0; } else { return 0; } key = line; data = &pos[1]; key = strip_white_space(key); data = strip_white_space(data); data = strip_off_quotes(data); if (!*key || !*data) { LOG_FATAL("Configuration parse error on line %d", line_count); return -1; } LOG_DUMP("config_parse_line: '%s' => '%s'", key, data); return apply_config(config, key, data, line_count); }
sid_t sid_alloc(struct sid_pool* pool, struct hub_user* user) { sid_t n; if (pool->count >= (pool->max - pool->min)) { #ifdef DEBUG_SID LOG_DUMP("SID_POOL: alloc, sid pool is full."); #endif return 0; } n = (++pool->count); for (; (pool->map[n % pool->max]); n++) ; #ifdef DEBUG_SID LOG_DUMP("SID_ALLOC: %d, user=%p", (int) n, user); #endif pool->map[n] = user; return n; }
//Compute MODBUS CRC16 for the packet uint16_t Packet::ComputeCRC() { //this part is borrowed from somewhere.... byte data[this->Size()-2]; this->ToArray(data); uint16_t crc = 0xFFFF; for (byte pos = 0; pos < this->Size()-2; pos++) { LOG_DUMP(F("CRC'ing byte: ")); LOG_DUMP(data[pos]); LOG_DUMP("\r\n"); crc ^= (uint16_t)data[pos]; // XOR byte into least sig. byte of crc for (int i = 8; i != 0; i--) { // Loop over each bit if ((crc & 0x0001) != 0) { // If the LSB is set crc >>= 1; // Shift right and XOR 0xA001 crc ^= 0xA001; } else { // Else LSB is not set crc >>= 1; // Just shift right } }
//---------------------------------------------------------------------------- static void ResMgr_DumpLockInfo(void) { MUINT32 i; // LOG_DUMP("E\r\n"); for(i=0;i<RES_MGR_ITEM_AMOUNT;i++) { if( ResMgr.LockInfo[i].Pid != 0 && ResMgr.LockInfo[i].Tgid != 0) { LOG_DUMP("Item(%ld,%s),Pid(%d),Tgid(%d),TaskName(%s),Jiffies(%ld),Time(%ld.%ld)\r\n", i, ResMgrItemName[i], ResMgr.LockInfo[i].Pid, ResMgr.LockInfo[i].Tgid, ResMgr.LockInfo[i].TaskName, ResMgr.LockInfo[i].Jiffies, ResMgr.LockInfo[i].TimeS, ResMgr.LockInfo[i].TimeUS); } } LOG_DUMP("X\r\n"); }
/**************************************************************************** ** ** ** Name: user_api_set_data() ** ** ** ** Description: Set content of data received buffer to allow loop back ** ** ** ** Inputs: message: Message to set into the received buffer ** ** ** ** Outputs: Return: The number of bytes write when success; ** ** RETURNerror Otherwise ** ** Others: _user_api_recv_buffer ** ** ** ***************************************************************************/ int user_api_set_data(char *message) { LOG_FUNC_IN; int rbytes; memset(_user_api_recv_buffer, 0, USER_API_RECV_BUFFER_SIZE); strncpy(_user_api_recv_buffer, message, USER_API_RECV_BUFFER_SIZE); rbytes = strlen(_user_api_recv_buffer); LOG_TRACE(INFO, "USR-API - %d bytes write", rbytes); LOG_DUMP(_user_api_recv_buffer, rbytes); LOG_FUNC_RETURN (rbytes); }
bool BufferedSocket::send(const char* data, size_t length) { volatile int sock = _sock; if(sock>=0) { if(sock==0) { LOG_FATAL("Invalid sock.`0`."); } _err = 0; LOG_DEBUG("Sending packet(%d): length=%u", sock, length); LOG_DUMP(data, length); if(::send(sock, data, length, 0)<0) { set_error(); return false; } return true; } return false; }
/**************************************************************************** ** ** ** Name: user_api_send_data() ** ** ** ** Description: Send data to the user application layer ** ** ** ** Inputs: fd: File descriptor of the connection endpoint ** ** to which data have to be sent ** ** length: Number of bytes to send ** ** Others: _user_api_send_buffer, _user_api_id ** ** ** ** Outputs: Return: The number of bytes sent when success; ** ** RETURNerror Otherwise ** ** Others: None ** ** ** ***************************************************************************/ static int _user_api_send_data(int length) { int sbytes = USER_API_SEND(_user_api_send_buffer, length); if (sbytes == RETURNerror) { LOG_TRACE(ERROR, "USR-API - send() failed, %s", strerror(errno)); return RETURNerror; } else if (sbytes == 0) { LOG_TRACE(WARNING, "USR-API - A signal was caught"); } else { LOG_TRACE(INFO, "USR-API - %d bytes sent " "to the user application layer", sbytes); LOG_DUMP(_user_api_send_buffer, sbytes); } return sbytes; }
int read_config(const char* file, struct hub_config* config, int allow_missing) { int ret; memset(config, 0, sizeof(struct hub_config)); config_defaults(config); ret = file_read_lines(file, config, &config_parse_line); if (ret < 0) { if (allow_missing && ret == -2) { LOG_DUMP("Using default configuration."); } else { return -1; } } return 0; }
struct sid_pool* sid_pool_create(sid_t max) { struct sid_pool* pool = hub_malloc(sizeof(struct sid_pool)); if (!pool) return 0; pool->min = 1; pool->max = max + 1; pool->count = 0; pool->map = hub_malloc_zero(sizeof(struct hub_user*) * pool->max); if (!pool->map) { hub_free(pool); return 0; } pool->map[0] = (struct hub_user*) pool; /* hack to reserve the first sid. */ #ifdef DEBUG_SID LOG_DUMP("SID_POOL: max=%d", (int) pool->max); #endif return pool; }
void Packet::SetDebugPort(SoftwareSerial* port) { LOG_DUMP(F("Packet::SetDebugPort()\r\n")); debugPort = port; }
static ssize_t ADC_client_recv(struct ADC_client* client) { static char buf[BIG_BUFSIZE]; struct ioq_recv* q = client->recv_queue; size_t buf_size = ioq_recv_get(q, buf, BIG_BUFSIZE); ssize_t size; ADC_TRACE; if (client->flags & cflag_choke) buf_size = 0; size = net_con_recv(client->con, buf + buf_size, BIG_BUFSIZE - buf_size); if (size > 0) buf_size += size; if (size < 0) return -1; else if (size == 0) return 0; else { char* lastPos = 0; char* start = buf; char* pos = 0; size_t remaining = buf_size; while ((pos = memchr(start, '\n', remaining))) { lastPos = pos+1; pos[0] = '\0'; #ifdef DEBUG_SENDQ LOG_DUMP("PROC: \"%s\" (%d)\n", start, (int) (pos - start)); #endif if (client->flags & cflag_choke) client->flags &= ~cflag_choke; else { if (((pos - start) > 0) && MAX_RECV_BUFFER > (pos - start)) { if (ADC_client_on_recv_line(client, start, pos - start) == -1) return -1; } } pos[0] = '\n'; /* FIXME: not needed */ pos ++; remaining -= (pos - start); start = pos; } if (lastPos || remaining) { if (remaining < (size_t) MAX_RECV_BUFFER) { ioq_recv_set(q, lastPos ? lastPos : buf, remaining); } else { ioq_recv_set(q, 0, 0); client->flags |= cflag_choke; LOG_WARN("Received message past MAX_RECV_BUFFER (%d), dropping message.", MAX_RECV_BUFFER); } } else { ioq_recv_set(q, 0, 0); } } return 0; }
int handle_net_read(struct hub_user* user) { static char buf[MAX_RECV_BUF]; struct hub_recvq* q = user->recv_queue; size_t buf_size = hub_recvq_get(q, buf, MAX_RECV_BUF); ssize_t size; if (user_flag_get(user, flag_maxbuf)) buf_size = 0; size = net_con_recv(user->connection, buf + buf_size, MAX_RECV_BUF - buf_size); if (size > 0) buf_size += size; if (size < 0) { if (size == -1) return quit_disconnected; else return quit_socket_error; } else if (size == 0) { return 0; } else { char* lastPos = 0; char* start = buf; char* pos = 0; size_t remaining = buf_size; while ((pos = memchr(start, '\n', remaining))) { lastPos = pos; pos[0] = '\0'; #ifdef DEBUG_SENDQ LOG_DUMP("PROC: \"%s\" (%d)\n", start, (int) (pos - start)); #endif if (user_flag_get(user, flag_maxbuf)) { user_flag_unset(user, flag_maxbuf); } else { if (((pos - start) > 0) && g_hub->config->max_recv_buffer > (pos - start)) { if (hub_handle_message(g_hub, user, start, (pos - start)) == -1) { return quit_protocol_error; } } } pos[0] = '\n'; /* FIXME: not needed */ pos ++; remaining -= (pos - start); start = pos; } if (lastPos || remaining) { if (remaining < g_hub->config->max_recv_buffer) { hub_recvq_set(q, lastPos ? lastPos : buf, remaining); } else { hub_recvq_set(q, 0, 0); user_flag_set(user, flag_maxbuf); LOG_WARN("Received message past max_recv_buffer, dropping message."); } } else { hub_recvq_set(q, 0, 0); } } return 0; }
static void eq_debug(const char* prefix, struct event_data* data) { LOG_DUMP(">>> %s: %p, id: %x, flags=%d\n", prefix, data, data->id, data->flags); }