Avnet AT&T IoT Starter Kit demo for Pubnub

Dependencies:   FXOS8700CQ mbed

Committer:
sveljko
Date:
Tue Aug 30 22:02:03 2016 +0000
Revision:
35:d0d86a2bcdb3
First version that works both for publish and subscribe. There are some issues, needs more testing, but, it works.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sveljko 35:d0d86a2bcdb3 1 #if !defined INC_PUBNUB
sveljko 35:d0d86a2bcdb3 2 #define INC_PUBNUB
sveljko 35:d0d86a2bcdb3 3
sveljko 35:d0d86a2bcdb3 4 #include <string>
sveljko 35:d0d86a2bcdb3 5 #include <vector>
sveljko 35:d0d86a2bcdb3 6
sveljko 35:d0d86a2bcdb3 7
sveljko 35:d0d86a2bcdb3 8 /** A pubnub context.
sveljko 35:d0d86a2bcdb3 9 */
sveljko 35:d0d86a2bcdb3 10 class pubnub_ctx {
sveljko 35:d0d86a2bcdb3 11 public:
sveljko 35:d0d86a2bcdb3 12 /** Give publish key as @p pub_key and subscribe key as @p key_sub
sveljko 35:d0d86a2bcdb3 13 to initialize the context.
sveljko 35:d0d86a2bcdb3 14 */
sveljko 35:d0d86a2bcdb3 15 pubnub_ctx(char const* pub_key, char const* key_sub);
sveljko 35:d0d86a2bcdb3 16
sveljko 35:d0d86a2bcdb3 17 ~pubnub_ctx();
sveljko 35:d0d86a2bcdb3 18
sveljko 35:d0d86a2bcdb3 19 /** Result of a Pubnub transaction */
sveljko 35:d0d86a2bcdb3 20 enum result {
sveljko 35:d0d86a2bcdb3 21 format_error,
sveljko 35:d0d86a2bcdb3 22 response_too_short,
sveljko 35:d0d86a2bcdb3 23 missing_open_bracket,
sveljko 35:d0d86a2bcdb3 24 missing_close_bracket,
sveljko 35:d0d86a2bcdb3 25 missing_time_token,
sveljko 35:d0d86a2bcdb3 26 bad_time_token,
sveljko 35:d0d86a2bcdb3 27 publish_failed,
sveljko 35:d0d86a2bcdb3 28 ok
sveljko 35:d0d86a2bcdb3 29 };
sveljko 35:d0d86a2bcdb3 30
sveljko 35:d0d86a2bcdb3 31 /** Publish a JSON @p message to a pubnub @p channel (or channels,
sveljko 35:d0d86a2bcdb3 32 separated by commas).
sveljko 35:d0d86a2bcdb3 33 This is a synchronous call, so it will take a while to return
sveljko 35:d0d86a2bcdb3 34 (depending on your Internet connection, could be 100s of ms).
sveljko 35:d0d86a2bcdb3 35 */
sveljko 35:d0d86a2bcdb3 36 result publish(char const* channel, char const* message);
sveljko 35:d0d86a2bcdb3 37
sveljko 35:d0d86a2bcdb3 38 /** Subscribe on a @p channel (or channels, separated by commas).
sveljko 35:d0d86a2bcdb3 39 Messages received (if any) are pushed back to the @p messages
sveljko 35:d0d86a2bcdb3 40 vector.
sveljko 35:d0d86a2bcdb3 41
sveljko 35:d0d86a2bcdb3 42 This is a synchronous call, but it will not wait the full Pubnub ~5 min
sveljko 35:d0d86a2bcdb3 43 for a message to appear on the channel. It will be much less, ~1 s.
sveljko 35:d0d86a2bcdb3 44
sveljko 35:d0d86a2bcdb3 45 @return vector of messages - empty if there are no messages (like on
sveljko 35:d0d86a2bcdb3 46 the very first subscribe)
sveljko 35:d0d86a2bcdb3 47 */
sveljko 35:d0d86a2bcdb3 48 result subscribe(char const* channel, std::vector<std::string>& messages);
sveljko 35:d0d86a2bcdb3 49
sveljko 35:d0d86a2bcdb3 50 std::string uuid() const { return d_uuid; }
sveljko 35:d0d86a2bcdb3 51 void set_uuid(char const *s) { d_uuid = s; }
sveljko 35:d0d86a2bcdb3 52
sveljko 35:d0d86a2bcdb3 53 std::string auth() const { return d_auth; }
sveljko 35:d0d86a2bcdb3 54 void set_auth(char const *s) { d_auth = s; }
sveljko 35:d0d86a2bcdb3 55
sveljko 35:d0d86a2bcdb3 56 private:
sveljko 35:d0d86a2bcdb3 57 /// The publish key to use
sveljko 35:d0d86a2bcdb3 58 std::string d_pub_key;
sveljko 35:d0d86a2bcdb3 59
sveljko 35:d0d86a2bcdb3 60 /// The subscribe key to use
sveljko 35:d0d86a2bcdb3 61 std::string d_key_sub;
sveljko 35:d0d86a2bcdb3 62
sveljko 35:d0d86a2bcdb3 63 /// the last timetoken
sveljko 35:d0d86a2bcdb3 64 std::string d_token;
sveljko 35:d0d86a2bcdb3 65
sveljko 35:d0d86a2bcdb3 66 /// The UUID to use (empty - do not use)
sveljko 35:d0d86a2bcdb3 67 std::string d_uuid;
sveljko 35:d0d86a2bcdb3 68
sveljko 35:d0d86a2bcdb3 69 /// The auth key to use (empty - do not use)
sveljko 35:d0d86a2bcdb3 70 std::string d_auth;
sveljko 35:d0d86a2bcdb3 71 };
sveljko 35:d0d86a2bcdb3 72
sveljko 35:d0d86a2bcdb3 73 #endif // !defined INC_PUBNUB