Skip to content

Commit cb5e66e

Browse files
committed
implemented signature function for devices
1 parent f952417 commit cb5e66e

File tree

3 files changed

+46
-35
lines changed

3 files changed

+46
-35
lines changed

src/SinricPro.h

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
#include "SinricProDeviceInterface.h"
1111
#include "SinricProInterface.h"
1212
#include "SinricProMessageid.h"
13+
#include "SinricProModuleCommandHandler.h"
1314
#include "SinricProNamespace.h"
1415
#include "SinricProQueue.h"
1516
#include "SinricProSignature.h"
1617
#include "SinricProStrings.h"
1718
#include "SinricProUDP.h"
1819
#include "SinricProWebsocket.h"
1920
#include "Timestamp.h"
20-
#include "SinricProModuleCommandHandler.h"
2121
namespace SINRICPRO_NAMESPACE {
2222

2323
/**
@@ -57,7 +57,7 @@ using OTAUpdateCallbackHandler = std::function<bool(const String& url, int major
5757
* @brief Function signature for setting a module setting.
5858
*
5959
* This callback is used to set a value for a specific setting identified by its ID.
60-
*
60+
*
6161
* @param id The unique identifier of the setting to be set.
6262
* @param value The new value to be assigned to the setting.
6363
* @return bool Returns true if the setting was successfully updated, false otherwise.
@@ -93,20 +93,21 @@ class SinricProClass : public SinricProInterface {
9393
class Proxy;
9494

9595
public:
96-
void begin(String appKey, String appSecret, String serverURL = SINRICPRO_SERVER_URL);
97-
void handle();
98-
void stop();
99-
bool isConnected();
100-
void onConnected(ConnectedCallbackHandler cb);
101-
void onDisconnected(DisconnectedCallbackHandler cb);
102-
void onPong(PongCallback cb);
103-
void restoreDeviceStates(bool flag);
104-
void setResponseMessage(String&& message);
105-
unsigned long getTimestamp() override;
106-
Proxy operator[](const String deviceId);
107-
void onOTAUpdate(OTAUpdateCallbackHandler cb);
108-
void onSetSetting(SetSettingCallbackHandler cb);
109-
void onReportHealth(ReportHealthCallbackHandler cb);
96+
void begin(String appKey, String appSecret, String serverURL = SINRICPRO_SERVER_URL);
97+
void handle();
98+
void stop();
99+
bool isConnected();
100+
void onConnected(ConnectedCallbackHandler cb);
101+
void onDisconnected(DisconnectedCallbackHandler cb);
102+
void onPong(PongCallback cb);
103+
void restoreDeviceStates(bool flag);
104+
void setResponseMessage(String&& message);
105+
unsigned long getTimestamp() override;
106+
virtual String sign(const String& message);
107+
Proxy operator[](const String deviceId);
108+
void onOTAUpdate(OTAUpdateCallbackHandler cb);
109+
void onSetSetting(SetSettingCallbackHandler cb);
110+
void onReportHealth(ReportHealthCallbackHandler cb);
110111

111112
protected:
112113
template <typename DeviceType>
@@ -117,7 +118,7 @@ class SinricProClass : public SinricProInterface {
117118

118119
JsonDocument prepareResponse(JsonDocument& requestMessage);
119120
JsonDocument prepareEvent(String deviceId, const char* action, const char* cause) override;
120-
void sendMessage(JsonDocument& jsonMessage) override;
121+
void sendMessage(JsonDocument& jsonMessage) override;
121122

122123
private:
123124
void handleReceiveQueue();
@@ -301,7 +302,7 @@ void SinricProClass::handle() {
301302

302303
JsonDocument SinricProClass::prepareRequest(String deviceId, const char* action) {
303304
JsonDocument requestMessage;
304-
JsonObject header = requestMessage[FSTR_SINRICPRO_header].to<JsonObject>();
305+
JsonObject header = requestMessage[FSTR_SINRICPRO_header].to<JsonObject>();
305306
header[FSTR_SINRICPRO_payloadVersion] = 2;
306307
header[FSTR_SINRICPRO_signatureVersion] = 1;
307308

@@ -332,20 +333,20 @@ void SinricProClass::handleModuleRequest(JsonDocument& requestMessage, interface
332333
#endif
333334

334335
JsonDocument responseMessage = prepareResponse(requestMessage);
335-
336-
String action = requestMessage[FSTR_SINRICPRO_payload][FSTR_SINRICPRO_action] | "";
337-
JsonObject request_value = requestMessage[FSTR_SINRICPRO_payload][FSTR_SINRICPRO_value];
338-
JsonObject response_value = responseMessage[FSTR_SINRICPRO_payload][FSTR_SINRICPRO_value];
339-
SinricProRequest request{ action, "", request_value, response_value};
336+
337+
String action = requestMessage[FSTR_SINRICPRO_payload][FSTR_SINRICPRO_action] | "";
338+
JsonObject request_value = requestMessage[FSTR_SINRICPRO_payload][FSTR_SINRICPRO_value];
339+
JsonObject response_value = responseMessage[FSTR_SINRICPRO_payload][FSTR_SINRICPRO_value];
340+
SinricProRequest request{action, "", request_value, response_value};
340341

341342
bool success = _moduleCommandHandler.handleRequest(request);
342-
343+
343344
responseMessage[FSTR_SINRICPRO_payload][FSTR_SINRICPRO_success] = success;
344345
responseMessage[FSTR_SINRICPRO_payload].remove(FSTR_SINRICPRO_deviceId);
345346
if (!success) {
346347
if (responseMessageStr.length() > 0) {
347348
responseMessage[FSTR_SINRICPRO_payload][FSTR_SINRICPRO_message] = responseMessageStr;
348-
responseMessageStr = "";
349+
responseMessageStr = "";
349350
} else {
350351
responseMessage[FSTR_SINRICPRO_payload][FSTR_SINRICPRO_message] = "Module did not handle \"" + action + "\"";
351352
}
@@ -424,13 +425,13 @@ void SinricProClass::handleReceiveQueue() {
424425
DEBUG_SINRIC("[SinricPro.handleReceiveQueue()]: Signature is valid. Processing message...\r\n");
425426
extractTimestamp(jsonMessage);
426427
if (messageType == FSTR_SINRICPRO_response) handleResponse(jsonMessage);
427-
if (messageType == FSTR_SINRICPRO_request) {
428+
if (messageType == FSTR_SINRICPRO_request) {
428429
String scope = jsonMessage[FSTR_SINRICPRO_payload][FSTR_SINRICPRO_scope] | FSTR_SINRICPRO_device;
429430
if (strcmp(FSTR_SINRICPRO_module, scope.c_str()) == 0) {
430431
handleModuleRequest(jsonMessage, rawMessage->getInterface());
431432
} else {
432433
handleDeviceRequest(jsonMessage, rawMessage->getInterface());
433-
}
434+
}
434435
};
435436
} else {
436437
DEBUG_SINRIC("[SinricPro.handleReceiveQueue()]: Signature is invalid! \r\n");
@@ -505,7 +506,7 @@ bool SinricProClass::isConnected() {
505506

506507
/**
507508
* @brief Set callback function for OTA (Over-The-Air) updates.
508-
*
509+
*
509510
* This method registers a callback function that will be called when an OTA update is available.
510511
* The callback should handle the process of downloading and applying the update.
511512
*
@@ -519,7 +520,7 @@ void SinricProClass::onOTAUpdate(OTAUpdateCallbackHandler cb) {
519520

520521
/**
521522
* @brief Set callback function for setting a module setting.
522-
*
523+
*
523524
* This method registers a callback function that will be called when a request to change
524525
* a module setting is received.
525526
* @return void
@@ -538,7 +539,7 @@ void SinricProClass::onSetSetting(SetSettingCallbackHandler cb) {
538539
* when the SinricPro system needs to report the device's health status.
539540
*
540541
* @param cb A function pointer of type ReportHealthCallbackHandler.
541-
* This callback should populate a String with health information and return a boolean
542+
* This callback should populate a String with health information and return a boolean
542543
* indicating success or failure of the health reporting process.
543544
* @return void
544545
* @see ReportHealthCallbackHandler for the definition of the callback function type.
@@ -660,17 +661,21 @@ void SinricProClass::setResponseMessage(String&& message) {
660661
}
661662

662663
/**
663-
* @brief Get the current timestamp
664+
* @brief
664665
*
665-
* @return unsigned long current timestamp (unix epoch time)
666+
* return unsigned long current timestamp(unix epoch time) * /
666667
*/
667668
unsigned long SinricProClass::getTimestamp() {
668669
return timestamp.getTimestamp();
669670
}
670671

672+
String SinricProClass::sign(const String& message) {
673+
return HMACbase64(message, appSecret);
674+
}
675+
671676
JsonDocument SinricProClass::prepareResponse(JsonDocument& requestMessage) {
672677
JsonDocument responseMessage;
673-
JsonObject header = responseMessage[FSTR_SINRICPRO_header].to<JsonObject>();
678+
JsonObject header = responseMessage[FSTR_SINRICPRO_header].to<JsonObject>();
674679
header[FSTR_SINRICPRO_payloadVersion] = 2;
675680
header[FSTR_SINRICPRO_signatureVersion] = 1;
676681

@@ -691,7 +696,7 @@ JsonDocument SinricProClass::prepareResponse(JsonDocument& requestMessage) {
691696

692697
JsonDocument SinricProClass::prepareEvent(String deviceId, const char* action, const char* cause) {
693698
JsonDocument eventMessage;
694-
JsonObject header = eventMessage[FSTR_SINRICPRO_header].to<JsonObject>();
699+
JsonObject header = eventMessage[FSTR_SINRICPRO_header].to<JsonObject>();
695700
header[FSTR_SINRICPRO_payloadVersion] = 2;
696701
header[FSTR_SINRICPRO_signatureVersion] = 1;
697702

src/SinricProDevice.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ class SinricProDevice : public SinricProDeviceInterface {
3333

3434
void registerRequestHandler(const SinricProRequestHandler &requestHandler);
3535
unsigned long getTimestamp();
36+
String sign(const String& message);
3637
virtual bool sendEvent(JsonDocument &event);
37-
virtual JsonDocument prepareEvent(const char *action, const char *cause);
38+
virtual JsonDocument prepareEvent(const char *action, const char *cause);
3839

3940
virtual String getProductType();
4041
virtual void begin(SinricProInterface *eventSender);
@@ -74,6 +75,10 @@ JsonDocument SinricProDevice::prepareEvent(const char* action, const char* cause
7475
return JsonDocument();
7576
}
7677

78+
String SinricProDevice::sign(const String& message) {
79+
if (eventSender) return eventSender->sign(message);
80+
return "";
81+
}
7782

7883
bool SinricProDevice::sendEvent(JsonDocument& event) {
7984
if (!SinricPro.isConnected()) {

src/SinricProInterface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class SinricProInterface {
1717
friend class SinricProDevice;
1818
protected:
1919
virtual void sendMessage(JsonDocument& jsonEvent);
20+
virtual String sign(const String& message);
2021
virtual JsonDocument prepareEvent(String deviceId, const char* action, const char* cause);
2122
virtual unsigned long getTimestamp();
2223
virtual bool isConnected();

0 commit comments

Comments
 (0)