Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTGWConnectionHandler.cpp Source File

MQTTGWConnectionHandler.cpp

00001 /**************************************************************************************
00002  * Copyright (c) 2016, Tomoaki Yamaguchi
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Tomoaki Yamaguchi - initial API and implementation and/or initial documentation
00015  **************************************************************************************/
00016 
00017 #include "MQTTGWConnectionHandler.h"
00018 #include "MQTTGWPacket.h"
00019 
00020 using namespace std;
00021 using namespace MQTTSNGW;
00022 
00023 MQTTGWConnectionHandler::MQTTGWConnectionHandler(Gateway* gateway)
00024 {
00025     _gateway = gateway;
00026 }
00027 
00028 MQTTGWConnectionHandler::~MQTTGWConnectionHandler()
00029 {
00030 
00031 }
00032 
00033 void MQTTGWConnectionHandler::handleConnack(Client* client, MQTTGWPacket* packet)
00034 {
00035     uint8_t rc = MQTT_SERVER_UNAVAILABLE;
00036     Connack resp;
00037     packet->getCONNACK(&resp);
00038 
00039     /* convert MQTT ReturnCode to MQTT-SN one */
00040     if (resp.rc == MQTT_CONNECTION_ACCEPTED)
00041     {
00042         rc = MQTTSN_RC_ACCEPTED;
00043     }
00044     else if (resp.rc == MQTT_UNACCEPTABLE_PROTOCOL_VERSION)
00045     {
00046         rc = MQTTSN_RC_NOT_SUPPORTED;
00047         WRITELOG(" ClientID : %s Requested Protocol version is not supported.\n", client->getClientId());
00048     }
00049     else if (resp.rc == MQTT_IDENTIFIER_REJECTED)
00050     {
00051         rc = MQTTSN_RC_NOT_SUPPORTED;
00052         WRITELOG(" ClientID : %s ClientID is collect UTF-8 but not allowed by the Server.\n",
00053                 client->getClientId());
00054     }
00055     else if (resp.rc == MQTT_SERVER_UNAVAILABLE)
00056     {
00057         rc = MQTTSN_RC_REJECTED_CONGESTED;
00058         WRITELOG(" ClientID : %s The Network Connection has been made but the MQTT service is unavailable.\n",
00059                 client->getClientId());
00060     }
00061     else if (resp.rc == MQTT_BAD_USERNAME_OR_PASSWORD)
00062     {
00063         rc = MQTTSN_RC_NOT_SUPPORTED;
00064         WRITELOG(" Gateway Configuration Error: The data in the user name or password is malformed.\n");
00065     }
00066     else if (resp.rc == MQTT_NOT_AUTHORIZED)
00067     {
00068         rc = MQTTSN_RC_NOT_SUPPORTED;
00069         WRITELOG(" Gateway Configuration Error: The Client is not authorized to connect.\n");
00070     }
00071 
00072     MQTTSNPacket* snPacket = new MQTTSNPacket();
00073     snPacket->setCONNACK(rc);
00074 
00075     Event* ev1 = new Event();
00076     ev1->setClientSendEvent(client, snPacket);
00077     client->connackSended(rc);  // update the client's status
00078     _gateway->getClientSendQue()->post(ev1);
00079 }
00080 
00081 void MQTTGWConnectionHandler::handlePingresp(Client* client, MQTTGWPacket* packet)
00082 {
00083     MQTTSNPacket* snPacket = new MQTTSNPacket();
00084     snPacket->setPINGRESP();
00085     Event* ev1 = new Event();
00086     ev1->setClientSendEvent(client, snPacket);
00087     client->updateStatus(snPacket);
00088     _gateway->getClientSendQue()->post(ev1);
00089 }
00090 
00091 void MQTTGWConnectionHandler::handleDisconnect(Client* client, MQTTGWPacket* packet)
00092 {
00093         MQTTSNPacket* snPacket = new MQTTSNPacket();
00094         snPacket->setDISCONNECT(0);
00095         client->disconnected();
00096         client->getNetwork()->close();
00097         Event* ev1 = new Event();
00098         ev1->setClientSendEvent(client, snPacket);
00099 }