1 /***************************************************************************/ /**
2 * @file sl_wifi_basic_credentials.c
3 *******************************************************************************
4 * # License
5 * <b>Copyright 2024 Silicon Laboratories Inc. www.silabs.com</b>
6 *******************************************************************************
7 *
8 * SPDX-License-Identifier: Zlib
9 *
10 * The licensor of this software is Silicon Laboratories Inc.
11 *
12 * This software is provided 'as-is', without any express or implied
13 * warranty. In no event will the authors be held liable for any damages
14 * arising from the use of this software.
15 *
16 * Permission is granted to anyone to use this software for any purpose,
17 * including commercial applications, and to alter it and redistribute it
18 * freely, subject to the following restrictions:
19 *
20 * 1. The origin of this software must not be misrepresented; you must not
21 * claim that you wrote the original software. If you use this software
22 * in a product, an acknowledgment in the product documentation would be
23 * appreciated but is not required.
24 * 2. Altered source versions must be plainly marked as such, and must not be
25 * misrepresented as being the original software.
26 * 3. This notice may not be removed or altered from any source distribution.
27 *
28 ******************************************************************************/
29
30 #include <string.h>
31 #include "sl_wifi.h"
32 #include "sl_wifi_types.h"
33 #include "sl_common.h"
34
35 #ifndef SL_WIFI_MAX_CREDENTIAL_COUNT
36 #define SL_WIFI_MAX_CREDENTIAL_COUNT 12
37 #endif
38
39 typedef struct {
40 sl_wifi_credential_type_t type;
41 uint16_t data_length;
42 uint8_t data[];
43 } sl_wifi_basic_credential_entry_t;
44
45 static sl_wifi_basic_credential_entry_t *credentials[SL_WIFI_MAX_CREDENTIAL_COUNT] = { 0 };
46
sl_wifi_set_credential(sl_wifi_credential_id_t id,sl_wifi_credential_type_t type,const void * credential,uint32_t credential_length)47 sl_status_t sl_wifi_set_credential(sl_wifi_credential_id_t id,
48 sl_wifi_credential_type_t type,
49 const void *credential,
50 uint32_t credential_length)
51 {
52 sl_wifi_basic_credential_entry_t *entry = NULL;
53
54 if (id >= SL_WIFI_MAX_CREDENTIAL_COUNT) {
55 return SL_STATUS_INVALID_PARAMETER;
56 }
57
58 // Check if the credential is invalid parameter
59 if ((NULL == credential) || (0 == credential_length)) {
60 return SL_STATUS_INVALID_PARAMETER;
61 }
62
63 if (credentials[id] == NULL) {
64 credentials[id] = malloc(sizeof(sl_wifi_basic_credential_entry_t) + credential_length);
65 if (credentials[id] == NULL) {
66 return SL_STATUS_ALLOCATION_FAILED;
67 }
68 memset(credentials[id], 0, sizeof(sl_wifi_basic_credential_entry_t) + credential_length);
69 credentials[id]->data_length = (uint16_t)credential_length;
70 }
71 entry = credentials[id];
72 entry->type = type;
73 entry->data_length = (uint16_t)credential_length;
74 memcpy(entry->data, credential, entry->data_length);
75
76 return SL_STATUS_OK;
77 }
78
sl_wifi_get_credential(sl_wifi_credential_id_t id,sl_wifi_credential_type_t * type,void * credential,uint32_t * credential_length)79 sl_status_t sl_wifi_get_credential(sl_wifi_credential_id_t id,
80 sl_wifi_credential_type_t *type,
81 void *credential,
82 uint32_t *credential_length)
83 {
84 const sl_wifi_basic_credential_entry_t *entry = NULL;
85
86 // Check if the credential ID is invalid parameter
87 if (id >= SL_WIFI_MAX_CREDENTIAL_COUNT) {
88 return SL_STATUS_INVALID_PARAMETER;
89 }
90
91 // Check if the credential is invalid parameter
92 if ((NULL == credential) || (0 == *credential_length)) {
93 return SL_STATUS_INVALID_PARAMETER;
94 }
95
96 entry = credentials[id];
97
98 if (NULL == entry) {
99 return SL_STATUS_NOT_FOUND;
100 }
101
102 if (*credential_length < entry->data_length) {
103 return SL_STATUS_FAIL;
104 }
105
106 *type = entry->type;
107 *credential_length = entry->data_length;
108 memcpy(credential, entry->data, entry->data_length);
109
110 return SL_STATUS_OK;
111 }
112
sl_wifi_delete_credential(sl_wifi_credential_id_t id)113 sl_status_t sl_wifi_delete_credential(sl_wifi_credential_id_t id)
114 {
115 // Check if the credential ID is invalid parameter
116 if (id >= SL_WIFI_MAX_CREDENTIAL_COUNT) {
117 return SL_STATUS_INVALID_PARAMETER;
118 }
119
120 if (NULL != credentials[id]) {
121 free(credentials[id]);
122 credentials[id] = NULL;
123 }
124
125 return SL_STATUS_OK;
126 }
127