This is the Pubnub library for MBed2 ("classic") with the "sync" interface. It is based on the Pubnub C-core library.

Dependencies:   Pubnub_c_core_mbed2_pal

Dependents:   Pubnub_ATT_IoT_SK_WNC_sync Pubnub_ATT_IOT_SK_WNC_AnalogIn_Issue Pubnub_ATT_IoT_SK_WNC_sync_3_27_Fork pubnub_sync

Committer:
sveljko
Date:
Fri Nov 11 22:46:57 2016 +0000
Revision:
2:4d49720c7200
Added the module/function to srand() using the time returned from Pubnub's `time` operation (which can be started with pubnub_time())

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sveljko 2:4d49720c7200 1 #include "srand_from_pubnub_time.h"
sveljko 2:4d49720c7200 2
sveljko 2:4d49720c7200 3 #include "pubnub_coreapi.h"
sveljko 2:4d49720c7200 4 #include "pubnub_ntf_sync.h"
sveljko 2:4d49720c7200 5
sveljko 2:4d49720c7200 6 #include <stdlib.h>
sveljko 2:4d49720c7200 7 #include <string.h>
sveljko 2:4d49720c7200 8
sveljko 2:4d49720c7200 9
sveljko 2:4d49720c7200 10 int srand_from_pubnub_time(pubnub_t *pbp)
sveljko 2:4d49720c7200 11 {
sveljko 2:4d49720c7200 12 pubnub_res rslt = pubnub_time(pbp);
sveljko 2:4d49720c7200 13 if (rslt != PNR_STARTED) {
sveljko 2:4d49720c7200 14 return -1;
sveljko 2:4d49720c7200 15 }
sveljko 2:4d49720c7200 16 rslt = pubnub_await(pbp);
sveljko 2:4d49720c7200 17 if (rslt != PNR_OK) {
sveljko 2:4d49720c7200 18 return -1;
sveljko 2:4d49720c7200 19 }
sveljko 2:4d49720c7200 20 char const* pbtime = pubnub_get(pbp);
sveljko 2:4d49720c7200 21 if (0 == pbtime) {
sveljko 2:4d49720c7200 22 return -1;
sveljko 2:4d49720c7200 23 }
sveljko 2:4d49720c7200 24 size_t length_of_time = strlen(pbtime);
sveljko 2:4d49720c7200 25 if (0 == length_of_time) {
sveljko 2:4d49720c7200 26 return -1;
sveljko 2:4d49720c7200 27 }
sveljko 2:4d49720c7200 28 char const *s = pbtime + length_of_time - 1;
sveljko 2:4d49720c7200 29 unsigned int val_for_srand = 0;
sveljko 2:4d49720c7200 30 for (int i = 0; (i < 10) && (s > pbtime); ++i, --s) {
sveljko 2:4d49720c7200 31 val_for_srand = val_for_srand * 10 + *s - '0';
sveljko 2:4d49720c7200 32 }
sveljko 2:4d49720c7200 33
sveljko 2:4d49720c7200 34 srand(val_for_srand);
sveljko 2:4d49720c7200 35
sveljko 2:4d49720c7200 36 return 0;
sveljko 2:4d49720c7200 37 }