//****************************************************************************** /// \brief Read and deal with incoming command /// \return #mxt_rc static int handle_cmd(struct mxt_device *mxt, int sockfd) { int ret; uint16_t address; uint16_t count; struct mxt_buffer linebuf; char *line; int offset; ret = mxt_buf_init(&linebuf); if (ret) return ret; ret = readline(mxt, sockfd, &linebuf); if (ret) { mxt_dbg(mxt->ctx, "Error reading or peer closed socket"); goto free; } line = (char *)linebuf.data; if (strlen(line) == 0) { ret = MXT_SUCCESS; goto free; } mxt_verb(mxt->ctx, "%s", line); if (!strcmp(line, "SAT")) { mxt_info(mxt->ctx, "Server attached"); ret = MXT_SUCCESS; } else if (!strcmp(line, "SDT")) { mxt_info(mxt->ctx, "Server detached"); ret = MXT_SUCCESS; } else if (sscanf(line, "REA %" SCNu16 " %" SCNu16, &address, &count) == 2) { ret = bridge_rea_cmd(mxt, sockfd, address, count); } else if (sscanf(line, "WRI %" SCNu16 "%n", &address, &offset) == 1) { /* skip space */ offset += 1; ret = bridge_wri_cmd(mxt, sockfd, address, line + offset, strlen(line) - offset); } else { mxt_info(mxt->ctx, "Unrecognised cmd \"%s\"", line); ret = MXT_SUCCESS; } free: mxt_buf_free(&linebuf); return ret; }
//****************************************************************************** /// \brief Read and deal with incoming command /// \return #mxt_rc static int handle_cmd(struct mxt_device *mxt, struct bridge_context *bridge_ctx) { int ret; uint16_t address; uint16_t count; struct mxt_buffer linebuf; char *line; int offset; ret = mxt_buf_init(&linebuf); if (ret) return ret; ret = readline(mxt, bridge_ctx->sockfd, &linebuf); if (ret) { mxt_dbg(mxt->ctx, "Error reading or peer closed socket"); goto free; } line = (char *)linebuf.data; if (strlen(line) == 0) { ret = MXT_SUCCESS; goto free; } mxt_verb(mxt->ctx, "%s", line); if (!strcmp(line, "SAT")) { mxt_info(mxt->ctx, "Server attached"); ret = MXT_SUCCESS; } else if (!strcmp(line, "SDT")) { mxt_info(mxt->ctx, "Server detached"); ret = MXT_SUCCESS; } else if (sscanf(line, "REA %" SCNu16 " %" SCNu16, &address, &count) == 2) { ret = bridge_rea_cmd(mxt, bridge_ctx, address, count); } else if (sscanf(line, "WRI %" SCNu16 "%n", &address, &offset) == 1) { /* skip space */ offset += 1; ret = bridge_wri_cmd(mxt, bridge_ctx, address, line + offset, strlen(line) - offset); } else if (sscanf(line, "RST %" SCNu16 "%n", &address, &offset) == 1) { ret = bridge_handle_reset(mxt, bridge_ctx, address); ret = MXT_SUCCESS; } else if (sscanf(line, "MSGCFG %" SCNu16 "%n", &address, &offset) == 1) { mxt_info(mxt->ctx, "Configuring Messages"); bridge_ctx->msgs_enabled = true; const char * const msg = "MSGCFG OK\n"; ret = write(bridge_ctx->sockfd, msg, strlen(msg)); if (ret < 0) { mxt_err(mxt->ctx, "Socket write error: %s (%d)", strerror(errno), errno); ret = mxt_errno_to_rc(errno); } ret = MXT_SUCCESS; } else { mxt_info(mxt->ctx, "Unrecognised cmd \"%s\"", line); ret = MXT_SUCCESS; } free: mxt_buf_free(&linebuf); return ret; }