1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @file openauthsession_demo.c
18  *
19  * @brief Provides the sample code for opening an authentication session.
20  *
21  * @since 1.0
22  * @version 1.0
23  */
24 
25 // Device A:
26 
27 #include <stdio.h>
28 #include "inner_session.h"
29 #include "session.h"
30 
31 ConnectionAddr g_addrInfo; // Information about the connection between devices
32 const char *g_pkgNameA = "dms"; // Application bundle name of device A
33 const char *g_sessionNameA = "ohos.distributedschedule.dms.test";  // Session name of device A
34 
35 // Notify that the session is set up successfully.
OnSessionOpened(int sessionId,int result)36 static int OnSessionOpened(int sessionId, int result)
37 {
38     printf("session opened,sesison id = %d\r\n", sessionId);
39     return 0;
40 }
41 
42 // Notify that the session is closed.
OnSessionClosed(int sessionId)43 static void OnSessionClosed(int sessionId)
44 {
45     printf("session closed, session id = %d\r\n", sessionId);
46 }
47 
48 // Notify that byte data is received.
OnBytesReceived(int sessionId,const void * data,unsigned int len)49 static void OnBytesReceived(int sessionId, const void *data, unsigned int len)
50 {
51     printf("session bytes received, session id = %d\r\n", sessionId);
52 }
53 
54 // Notify that the message is received.
OnMessageReceived(int sessionId,const void * data,unsigned int len)55 static void OnMessageReceived(int sessionId, const void *data, unsigned int len)
56 {
57     printf("session msg received, session id = %d\r\n", sessionId);
58 }
59 
60 static ISessionListener g_sessionlistenerA = {
61     .OnSessionOpened = OnSessionOpened,
62     .OnSessionClosed = OnSessionClosed,
63     .OnBytesReceived = OnBytesReceived,
64     .OnMessageReceived = OnMessageReceived,
65 };
66 
main(void)67 int main(void)
68 {
69     /*
70      * 1. Device A calls CreateSessionServer() to create a session server based on
71      * the application bundle name and session name, and registers the callbacks for session opened, session closed,
72      * byte received, and message received.
73      */
74     int ret = CreateSessionServer(g_pkgNameA, g_sessionNameA, &g_sessionlistenerA);
75     printf("create session server result = %d\n", ret);
76 
77     /*
78      * 2. Device A calls OpenAuthSession() to create a raw channel for identity negotiation based on the session name
79      * and connection information before networking.
80      */
81     int sessionId = OpenAuthSession(g_sessionNameA, &(g_addrInfo), 1, NULL);
82     printf("open auth session result = %d\n", sessionId);
83 
84     /*
85      * 3. When the identity authentication is complete,
86      * NotifyAuthSuccess is called to notify device A of the authentication success.
87      */
88     NotifyAuthSuccess(sessionId);
89 
90     /* 4. After the authentication is successful, device A closes the session and removes the session server. */
91     ret = RemoveSessionServer(g_pkgNameB, g_sessionNameB);
92     printf("remove session server result = %d\n", ret);
93 }
94 
95 // Device B:
96 
97 #include <stdio.h>
98 #include "inner_session.h"
99 #include "session.h"
100 
101 const char *g_pkgNameB = "dmsB"; // Application bundle name of device B
102 const char *g_sessionNameB = "ohos.distributedschedule.dms.testB";  // Session name of device B
103 
OnSessionOpened(int sessionId,int result)104 static int OnSessionOpened(int sessionId, int result)
105 {
106     printf("session opened,sesison id = %d\r\n", sessionId);
107     return 0;
108 }
109 
OnSessionClosed(int sessionId)110 static void OnSessionClosed(int sessionId)
111 {
112     printf("session closed, session id = %d\r\n", sessionId);
113 }
114 
OnBytesReceived(int sessionId,const void * data,unsigned int len)115 static void OnBytesReceived(int sessionId, const void *data, unsigned int len)
116 {
117     printf("session bytes received, session id = %d\r\n", sessionId);
118 }
119 
OnMessageReceived(int sessionId,const void * data,unsigned int len)120 static void OnMessageReceived(int sessionId, const void *data, unsigned int len)
121 {
122     printf("session msg received, session id = %d\r\n", sessionId);
123 }
124 
125 static ISessionListener g_sessionlistenerB = {
126     .OnSessionOpened = OnSessionOpened,
127     .OnSessionClosed = OnSessionClosed,
128     .OnBytesReceived = OnBytesReceived,
129     .OnMessageReceived = OnMessageReceived,
130 };
131 
main(void)132 int main(void)
133 {
134     /*
135      * 1. Device B calls CreateSessionServer to create a session server based on
136      * the application bundle name and session name, and registers the callbacks for session opened and session closed.
137      */
138     int ret = CreateSessionServer(g_pkgNameB, g_sessionNameB, &g_sessionlistenerB);
139     printf("create session server result = %d\n", ret);
140 
141     /*
142      * 2. When device B receives information about the identity authentication session negotiation from device A,
143      * OnSessionOpened is called to notify device B that the identity authentication session is successfully opened.
144      */
145 
146     /* 3. After the authentication is successful, device B closes the session and removes the session server. */
147     ret = RemoveSessionServer(g_pkgNameB, g_sessionNameB);
148     printf("remove session server result = %d\n", ret);
149 }
150