forked from yodakohl/lssdp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlssdp.h
153 lines (108 loc) · 3.93 KB
/
lssdp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#ifndef __LSSDP_H
#define __LSSDP_H
#include <stdbool.h> // bool, true, false
#include <stdint.h> // uint32_t
#ifdef __cplusplus
extern "C"
{
#endif
#define LSSDP_IP_LEN 128
#define LSSDP_FIELD_LEN 128
#define LSSDP_LOCATION_LEN 256
// LSSDP Log Level
enum LSSDP_LOG {
LSSDP_LOG_DEBUG = 1 << 0,
LSSDP_LOG_INFO = 1 << 1,
LSSDP_LOG_WARN = 1 << 2,
LSSDP_LOG_ERROR = 1 << 3
};
/** Global Variable **/
typedef struct lssdp_config {
char* multicastPort;
const char * ADDR_LOCALHOST;
const char * ADDR_MULTICAST;
} lssdp_config;
typedef struct lssdp_nbr {
char usn [LSSDP_FIELD_LEN]; // Unique Service Name (Device Name or MAC)
char location [LSSDP_LOCATION_LEN]; // URL or IP(:Port)
/* Additional SSDP Header Fields */
char sm_id [LSSDP_FIELD_LEN];
char device_type [LSSDP_FIELD_LEN];
long long update_time;
struct lssdp_nbr * next;
} lssdp_nbr;
struct t_location { // Location (optional):
char prefix [LSSDP_FIELD_LEN]; // Protocal: "https://" or "http://"
char domain [LSSDP_FIELD_LEN]; // if domain is empty, using Interface IP as default
char suffix [LSSDP_FIELD_LEN]; // URI or Port: "/index.html" or ":80"
};
/** Struct: lssdp_packet **/
typedef struct lssdp_packet {
char method [LSSDP_FIELD_LEN]; // M-SEARCH, NOTIFY, RESPONSE, BYE
char st [LSSDP_FIELD_LEN]; // Search Target
char nts [LSSDP_FIELD_LEN]; // Search Target
char usn [LSSDP_FIELD_LEN]; // Unique Service Name
char location [LSSDP_LOCATION_LEN]; // Location
/* Additional SSDP Header Fields */
char sm_id [LSSDP_FIELD_LEN];
char device_type [LSSDP_FIELD_LEN];
int max_age;
long long update_time;
} lssdp_packet;
struct t_header {
/* SSDP Standard Header Fields */
char search_target [LSSDP_FIELD_LEN]; // Search Target
char unique_service_name [LSSDP_FIELD_LEN]; // Unique Service Name: MAC or User Name
int max_age;
struct t_location location;
/* Additional SSDP Header Fields */
char sm_id [LSSDP_FIELD_LEN];
char device_type [LSSDP_FIELD_LEN];
};
typedef struct lssdp_ctx {
int sock; // SSDP listening socket
unsigned short port; // SSDP port (0x0000 ~ 0xFFFF)
lssdp_nbr * neighbor_list; // SSDP neighbor list
long neighbor_timeout; // milliseconds
bool debug; // show debug log
struct lssdp_config config;
char* multicastPortTest;
struct t_header header;
int (* neighbor_list_changed_callback) (struct lssdp_ctx * lssdp);
int (* packet_received_callback) (struct lssdp_ctx * lssdp,
const char * packet, size_t packet_len);
} lssdp_ctx;
//Set default values of lssdp
void lssdp_init(lssdp_ctx * lssdp);
int lssdp_socket_create(lssdp_ctx * lssdp);
int lssdp_socket_close(lssdp_ctx * lssdp);
/*
* lssdp_socket_read
*
* read SSDP socket.
*
* 1. if read success, packet_received_callback will be invoked.
* 2. if received SSDP packet is match to Search Target (lssdp.header.search_target),
* - M-SEARCH: send RESPONSE back
* - NOTIFY/RESPONSE: add/update to SSDP neighbor list
*
* Note:
* - SSDP socket and port must be setup ready before call this function. (sock, port > 0)
*
* @param lssdp
* @return = 0 success
* < 0 failed
*/
int lssdp_socket_read(lssdp_ctx * lssdp);
int lssdp_send_msearch(lssdp_ctx * lssdp);
int lssdp_send_byebye(lssdp_ctx * lssdp);
int lssdp_send_notify(lssdp_ctx * lssdp);
int lssdp_packet_parser(const char * data, size_t data_len,
lssdp_packet * packet);
void lssdp_set_log_callback(void (* callback)(const char * file,
const char * tag, int level, int line, const char * func,
const char * message));
#ifdef __cplusplus
}
#endif
#endif