softbus_client_main.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include "securec.h"
  5. #include "discovery_service.h"
  6. #include "softbus_bus_center.h"
  7. #include "session.h"
  8. #define PACKAGE_NAME "softbus_sample"
  9. #define LOCAL_SESSION_NAME "session_test"
  10. #define TARGET_SESSION_NAME "session_test"
  11. #define DEFAULT_CAPABILITY "osdCapability"
  12. #define DEFAULT_SESSION_GROUP "group_test"
  13. #define DEFAULT_PUBLISH_ID 123
  14. static int g_sessionId;
  15. static void PublishSuccess(int publishId)
  16. {
  17. printf("<PublishSuccess>CB: publish %d done\n", publishId);
  18. }
  19. static void PublishFailed(int publishId, PublishFailReason reason)
  20. {
  21. printf("<PublishFailed>CB: publish %d failed, reason=%d\n", publishId, (int)reason);
  22. }
  23. static int PublishServiceInterface()
  24. {
  25. PublishInfo info = {
  26. .publishId = DEFAULT_PUBLISH_ID,
  27. .mode = DISCOVER_MODE_PASSIVE,
  28. .medium = COAP,
  29. .freq = LOW,
  30. .capability = DEFAULT_CAPABILITY,
  31. .capabilityData = NULL,
  32. .dataLen = 0,
  33. };
  34. IPublishCallback cb = {
  35. .OnPublishSuccess = PublishSuccess,
  36. .OnPublishFail = PublishFailed,
  37. };
  38. return PublishService(PACKAGE_NAME, &info, &cb);
  39. }
  40. static void UnPublishServiceInterface(void)
  41. {
  42. int ret = UnPublishService(PACKAGE_NAME, DEFAULT_PUBLISH_ID);
  43. if (ret != 0) {
  44. printf("UnPublishService fail:%d\n", ret);
  45. }
  46. }
  47. static void DeviceFound(const DeviceInfo *device)
  48. {
  49. unsigned int i;
  50. printf("<DeviceFound>CB: Device has found\n");
  51. printf("\tdevId=%s\n", device->devId);
  52. printf("\tdevName=%s\n", device->devName);
  53. printf("\tdevType=%d\n", device->devType);
  54. printf("\taddrNum=%d\n", device->addrNum);
  55. for (i = 0; i < device->addrNum; i++) {
  56. printf("\t\taddr%d:type=%d,", i + 1, device->addr[i].type);
  57. switch (device->addr[i].type) {
  58. case CONNECTION_ADDR_WLAN:
  59. case CONNECTION_ADDR_ETH:
  60. printf("ip=%s,port=%d,", device->addr[i].info.ip.ip, device->addr[i].info.ip.port);
  61. break;
  62. default:
  63. break;
  64. }
  65. printf("peerUid=%s\n", device->addr[i].peerUid);
  66. }
  67. printf("\tcapabilityBitmapNum=%d\n", device->capabilityBitmapNum);
  68. for (i = 0; i < device->addrNum; i++) {
  69. printf("\t\tcapabilityBitmap[%d]=0x%x\n", i + 1, device->capabilityBitmap[i]);
  70. }
  71. printf("\tcustData=%s\n", device->custData);
  72. }
  73. static void DiscoverySuccess(int subscribeId)
  74. {
  75. printf("<DiscoverySuccess>CB: discover subscribeId=%d\n", subscribeId);
  76. }
  77. static void DiscoveryFailed(int subscribeId, DiscoveryFailReason reason)
  78. {
  79. printf("<DiscoveryFailed>CB: discover subscribeId=%d failed, reason=%d\n", subscribeId, (int)reason);
  80. }
  81. static int DiscoveryInterface(void)
  82. {
  83. SubscribeInfo info = {
  84. .subscribeId = DEFAULT_PUBLISH_ID,
  85. .mode = DISCOVER_MODE_ACTIVE,
  86. .medium = COAP,
  87. .freq = LOW,
  88. .isSameAccount = false,
  89. .isWakeRemote = false,
  90. .capability = DEFAULT_CAPABILITY,
  91. .capabilityData = NULL,
  92. .dataLen = 0,
  93. };
  94. IDiscoveryCallback cb = {
  95. .OnDeviceFound = DeviceFound,
  96. .OnDiscoverFailed = DiscoveryFailed,
  97. .OnDiscoverySuccess = DiscoverySuccess,
  98. };
  99. return StartDiscovery(PACKAGE_NAME, &info, &cb);
  100. }
  101. static void StopDiscoveryInterface(void)
  102. {
  103. int ret = StopDiscovery(PACKAGE_NAME, DEFAULT_PUBLISH_ID);
  104. if (ret) {
  105. printf("StopDiscovery fail:%d\n", ret);
  106. }
  107. }
  108. static int SessionOpened(int sessionId, int result)
  109. {
  110. printf("<SessionOpened>CB: session %d open fail:%d\n", sessionId, result);
  111. if (result == 0) {
  112. g_sessionId = sessionId;
  113. }
  114. return result;
  115. }
  116. static void SessionClosed(int sessionId)
  117. {
  118. printf("<SessionClosed>CB: session %d closed\n", sessionId);
  119. }
  120. static void ByteRecived(int sessionId, const void *data, unsigned int dataLen)
  121. {
  122. printf("<ByteRecived>CB: session %d received %u bytes data=%s\n", sessionId, dataLen, (const char *)data);
  123. }
  124. static void MessageReceived(int sessionId, const void *data, unsigned int dataLen)
  125. {
  126. printf("<MessageReceived>CB: session %d received %u bytes message=%s\n", sessionId, dataLen, (const char *)data);
  127. }
  128. static int CreateSessionServerInterface(void)
  129. {
  130. const ISessionListener sessionCB = {
  131. .OnSessionOpened = SessionOpened,
  132. .OnSessionClosed = SessionClosed,
  133. .OnBytesReceived = ByteRecived,
  134. .OnMessageReceived = MessageReceived,
  135. };
  136. return CreateSessionServer(PACKAGE_NAME, LOCAL_SESSION_NAME, &sessionCB);
  137. }
  138. static void RemoveSessionServerInterface(void)
  139. {
  140. int ret = RemoveSessionServer(PACKAGE_NAME, LOCAL_SESSION_NAME);
  141. if (ret) {
  142. printf("RemoveSessionServer fail:%d\n", ret);
  143. }
  144. }
  145. static int OpenSessionInterface(const char *peerNetworkId)
  146. {
  147. SessionAttribute attr = {
  148. .dataType = TYPE_BYTES,
  149. .linkTypeNum = 1,
  150. .linkType[0] = LINK_TYPE_WIFI_WLAN_2G,
  151. .attr = {RAW_STREAM},
  152. };
  153. return OpenSession(LOCAL_SESSION_NAME, TARGET_SESSION_NAME, peerNetworkId, DEFAULT_SESSION_GROUP, &attr);
  154. }
  155. static void CloseSessionInterface(int sessionId)
  156. {
  157. CloseSession(sessionId);
  158. }
  159. static int GetAllNodeDeviceInfoInterface(NodeBasicInfo **dev)
  160. {
  161. int ret, num;
  162. ret = GetAllNodeDeviceInfo(PACKAGE_NAME, dev, &num);
  163. if (ret) {
  164. printf("GetAllNodeDeviceInfo fail:%d\n", ret);
  165. return -1;
  166. }
  167. printf("<GetAllNodeDeviceInfo>return %d Node\n", num);
  168. for (int i = 0; i < num; i++) {
  169. printf("<num %d>deviceName=%s\n", i + 1, dev[i]->deviceName);
  170. printf("\tnetworkId=%s\n", dev[i]->networkId);
  171. printf("\tType=%d\n", dev[i]->deviceTypeId);
  172. }
  173. return num;
  174. }
  175. static void FreeNodeInfoInterface(NodeBasicInfo *dev)
  176. {
  177. FreeNodeInfo(dev);
  178. }
  179. static void commnunicate(void)
  180. {
  181. NodeBasicInfo *dev = NULL;
  182. char cData[] = "hello world test";
  183. int dev_num, sessionId, input, ret;
  184. int timeout = 5;
  185. dev_num = GetAllNodeDeviceInfoInterface(&dev);
  186. if (dev_num <= 0) {
  187. return;
  188. }
  189. printf("\nInput Node num to commnunication:");
  190. scanf_s("%d", &input);
  191. if (input <= 0 || input > dev_num) {
  192. printf("error input num\n");
  193. goto err_input;
  194. }
  195. g_sessionId = -1;
  196. sessionId = OpenSessionInterface(dev[input - 1].networkId);
  197. if (sessionId < 0) {
  198. printf("OpenSessionInterface fail, ret=%d\n", sessionId);
  199. goto err_OpenSessionInterface;
  200. }
  201. while (timeout) {
  202. if (g_sessionId == sessionId) {
  203. ret = SendBytes(sessionId, cData, strlen(cData) + 1);
  204. if (ret) {
  205. printf("SendBytes fail:%d\n", ret);
  206. }
  207. break;
  208. }
  209. timeout--;
  210. sleep(1);
  211. }
  212. CloseSessionInterface(sessionId);
  213. err_OpenSessionInterface:
  214. err_input:
  215. FreeNodeInfoInterface(dev);
  216. }
  217. int main(int argc, char **argv)
  218. {
  219. bool loop = true;
  220. int ret;
  221. ret = CreateSessionServerInterface();
  222. if (ret) {
  223. printf("CreateSessionServer fail, ret=%d\n", ret);
  224. return ret;
  225. } else {
  226. printf("CreateSessionServer suc. ret=%d\n", ret);
  227. }
  228. ret = PublishServiceInterface();
  229. if (ret) {
  230. printf("PublishService fail, ret=%d\n", ret);
  231. goto err_PublishServiceInterface;
  232. } else {
  233. printf("PublishService suc. ret=%d\n", ret);
  234. }
  235. ret = DiscoveryInterface();
  236. if (ret) {
  237. printf("DiscoveryInterface fail, ret=%d\n", ret);
  238. goto err_DiscoveryInterface;
  239. } else {
  240. printf("DiscoveryInterface suc. ret=%d\n", ret);
  241. }
  242. while (loop) {
  243. printf("\nInput c to commnuication, Input s to stop:");
  244. char op = getchar();
  245. switch(op) {
  246. case 'c':
  247. commnunicate();
  248. continue;
  249. case 's':
  250. loop = false;
  251. break;
  252. case '\n':
  253. break;
  254. default:
  255. continue;
  256. }
  257. }
  258. StopDiscoveryInterface();
  259. err_DiscoveryInterface:
  260. UnPublishServiceInterface();
  261. err_PublishServiceInterface:
  262. RemoveSessionServerInterface();
  263. return 0;
  264. }