Exemplo n.º 1
0
//--------------------------------------------------------------------------------------------------
static void RxNewData
(
    int fd,      ///< [IN] File descriptor to read on
    short events ///< [IN] Event reported on fd
)
{
    char buffer[READ_BYTES];
    ssize_t count;

    SharedData_t* sharedDataPtr = le_fdMonitor_GetContextPtr();
    le_sem_Post(sharedDataPtr->semRef);

    memset(buffer, 0, sizeof(buffer));

    if (events & (POLLIN | POLLPRI))
    {
        count = read(fd, buffer, READ_BYTES);
        if (count == -1)
        {
            LE_ERROR("read error: %s", strerror(errno));
            return;
        }
        else if (count > 0)
        {
            if (strcmp(buffer, "AT+CREG?\r") == 0)
            {
                LE_INFO("Received AT command: %s", buffer);
                // Send the response of AT command
                write(fd, "\r\n\r\n+CREG: 0,1\r\n\r\n\r\nOK\r\n", 24);
                return;
            }
            else if (strcmp(buffer, "AT+CGSN\r") == 0)
            {
                LE_INFO("Received AT command: %s", buffer);
                // Send the response of AT command
                write(fd, "\r\n359377060033064\r\n\r\nOK\r\n", 25);
                return;
            }
        }
    }
}
Exemplo n.º 2
0
//--------------------------------------------------------------------------------------------------
static void FdEventHandler
(
    int fd,
    short events
)
//--------------------------------------------------------------------------------------------------
{
    Parser_t* parserPtr = le_fdMonitor_GetContextPtr();

    // Increment the reference count on the Parser object so it won't go away until we are done
    // with it, even if the client calls le_json_Cleanup() for this parser.
    le_mem_AddRef(parserPtr);

    if (events & POLLIN)    // Data available to read?
    {
        ReadData(parserPtr, fd);
    }

    // Error or hang-up?
    if (NotStopped(parserPtr) && ((events & POLLERR) || (events & POLLHUP) || (events & POLLRDHUP)))
    {
        char msg[256];
        size_t bytesPrinted = snprintf(msg, sizeof(msg), "Read error flags set:");
        if (events & POLLERR)
        {
            bytesPrinted += snprintf(msg + bytesPrinted, sizeof(msg) - bytesPrinted, " POLLERR");
        }
        if (events & POLLHUP)
        {
            bytesPrinted += snprintf(msg + bytesPrinted, sizeof(msg) - bytesPrinted, " POLLHUP");
        }
        if (events & POLLRDHUP)
        {
            bytesPrinted += snprintf(msg + bytesPrinted, sizeof(msg) - bytesPrinted, " POLLRDHUP");
        }
        Error(parserPtr, LE_JSON_READ_ERROR, msg);
    }

    // We are finished with the parser object now.
    le_mem_Release(parserPtr);
}
Exemplo n.º 3
0
static void mqttClient_socketFdEventHandler(int sockFd, short events)
{
  mqttClient_t* clientData = le_fdMonitor_GetContextPtr();
  int rc = LE_OK;

  LE_ASSERT(clientData);

  LE_DEBUG("events(0x%08x)", events);
  if (events & POLLOUT)
  {
    le_fdMonitor_Disable(clientData->session.sockFdMonitor, POLLOUT);

    if ((clientData->session.sock != MQTT_CLIENT_INVALID_SOCKET) && !clientData->session.isConnected)
    {
      LE_INFO("connected(%s:%d)", clientData->session.config.brokerUrl, clientData->session.config.portNumber);
      rc = le_timer_Stop(clientData->session.connTimer);
      if (rc)
      {
        LE_ERROR("le_timer_Stop() failed(%d)", rc);
        goto cleanup;
      }

      MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
      data.willFlag = 0;
      data.MQTTVersion = MQTT_CLIENT_MQTT_VERSION;

      LE_INFO("deviceId('%s'), pwd('%s')", clientData->deviceId, clientData->session.secret);
      if (strlen(clientData->deviceId) > 0)
      {
        data.clientID.cstring = clientData->deviceId;
        data.username.cstring = clientData->deviceId;
        data.password.cstring = clientData->session.secret;
      }

      data.keepAliveInterval = clientData->session.config.keepAlive;
      data.cleansession = 1;

      rc = mqttClient_sendConnect(clientData, &data);
      if (rc)
      {
        LE_ERROR("mqttClient_sendConnect() failed(%d)", rc);
        goto cleanup;
      }
    }
    else if (clientData->session.tx.bytesLeft)
    {
      rc = mqttClient_write(clientData, 0);
      if (rc)
      {
        LE_ERROR("mqttClient_write() failed(%d)", rc);
        goto cleanup;
      }
    }
  }
  else if (events & POLLIN)
  {
    uint16_t packetType = MQTTPacket_read(clientData->session.rx.buf, sizeof(clientData->session.rx.buf), mqttClient_read);    
    LE_DEBUG("packet type(%d)", packetType);
    switch (packetType)
    {
    case CONNACK:
      rc = mqttClient_processConnAck(clientData);
      if (rc)
      {
        LE_ERROR("mqttClient_processConnAck() failed(%d)", rc);
        goto cleanup;
      }

      break;

    case PUBACK:
      rc = mqttClient_processPubAck(clientData);
      if (rc)
      {
        LE_ERROR("mqttClient_processPubAck() failed(%d)", rc);
        goto cleanup;
      }

      break;

    case SUBACK:
      rc = mqttClient_processSubAck(clientData);
      if (rc)
      {
        LE_ERROR("mqttClient_processSubAck() failed(%d)", rc);
        goto cleanup;
      }

      break;

    case UNSUBACK:
      rc = mqttClient_processUnSubAck(clientData);
      if (rc)
      {
        LE_ERROR("mqttClient_processUnSubAck() failed(%d)", rc);
        goto cleanup;
      }

      break;

    case PUBLISH:
      rc = mqttClient_processPublish(clientData);
      if (rc)
      {
        LE_ERROR("mqttClient_processPublish() failed(%d)", rc);
        goto cleanup;
      }

      break;
    
    case PUBREC:
      rc = mqttClient_processPubRec(clientData);
      if (rc)
      {
        LE_ERROR("mqttClient_processPubRec() failed(%d)", rc);
        goto cleanup;
      }
  
      break;
    
    case PUBCOMP:
      rc = mqttClient_processPubComp(clientData);
      if (rc)
      {
        LE_ERROR("mqttClient_processPubComp() failed(%d)", rc);
        goto cleanup;
      }

      break;

    case PINGRESP:
      mqttClient_processPingResp(clientData);
      break;

    default:
      LE_ERROR("unknown packet type(%u)", packetType);
      break;
    }

    clientData->session.cmdRetries = 0;
  }

cleanup:
  return;
}