Morse Micro IoT SDK  2.9.7
dpp.c
Go to the documentation of this file.
1/*
2 * Copyright 2025 Morse Micro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
16#include <stdio.h>
17#include <string.h>
18#include "mmutils.h"
19#include "mmosal.h"
20#include "mmwlan.h"
21#include "mmconfig.h"
22#include "mm_app_common.h"
23
24
34{
35 switch (result)
36 {
38 return "Success";
40 return "Error";
42 return "Session Overlap";
43 default:
44 return "Unknown Result";
45 }
46}
47
54static void dpp_event_handler(const struct mmwlan_dpp_cb_args *dpp_event, void *arg)
55{
56 struct mmosal_semb *semb = (struct mmosal_semb *)arg;
57 if (dpp_event->event != MMWLAN_DPP_EVT_PB_RESULT)
58 {
59 mmosal_printf("Unsupported event %lu\n", dpp_event->event);
60 return;
61 }
62
63 mmosal_printf("DPP Result: %s\n",
65
67 {
68 return;
69 }
70
71 if ((dpp_event->args.pb_result.ssid == NULL) ||
72 (dpp_event->args.pb_result.passphrase == NULL) ||
73 (dpp_event->args.pb_result.ssid_len > MMWLAN_SSID_MAXLEN - 1))
74 {
75 mmosal_printf("Invalid/incomplete credentials provided\n");
76 return;
77 }
78
79 mmosal_printf("SSID %*s, PWD %s\n",
80 dpp_event->args.pb_result.ssid_len,
81 dpp_event->args.pb_result.ssid,
82 dpp_event->args.pb_result.passphrase);
83
84
85 char ssid[MMWLAN_SSID_MAXLEN];
86 memcpy(ssid, dpp_event->args.pb_result.ssid, dpp_event->args.pb_result.ssid_len);
87 ssid[dpp_event->args.pb_result.ssid_len] = '\0';
88 mmconfig_write_string("wlan.ssid", ssid);
89 mmconfig_write_string("wlan.password", dpp_event->args.pb_result.passphrase);
90
91 (void)mmosal_semb_give(semb);
92}
93
94
99void app_init(void)
100{
101 printf("\n\nSTA DPP Connect Example (Built " __DATE__ " " __TIME__ ")\n\n");
102
103 struct mmosal_semb *semb = mmosal_semb_create("dpp");
104 MMOSAL_ASSERT(semb);
105
107
108 struct mmwlan_dpp_args dpp_args = {.dpp_event_cb = dpp_event_handler, .dpp_event_cb_arg = semb};
109
110 mmosal_printf("DPP Start\n");
111 enum mmwlan_status status = mmwlan_dpp_start(&dpp_args);
112 MMOSAL_ASSERT(status == MMWLAN_SUCCESS);
113
114 bool ok = mmosal_semb_wait(semb, 200*1000);
115 MMOSAL_ASSERT(ok);
116
118
120}
121
const char * mmwlan_dpp_pb_result_to_string(enum mmwlan_dpp_pb_result result)
Convert a DPP push button result code to its string representation.
Definition: dpp.c:33
static void dpp_event_handler(const struct mmwlan_dpp_cb_args *dpp_event, void *arg)
Function to handle dpp events.
Definition: dpp.c:54
void app_init(void)
Main entry point to the application.
Definition: dpp.c:99
int mmconfig_write_string(const char *key, const char *value)
Writes the null terminated string to persistent store location identified by key.
#define MMOSAL_ASSERT(expr)
Assert that the given expression evaluates to true and abort execution if not.
Definition: mmosal.h:927
int mmosal_printf(const char *format,...)
OS abstracted version of printf used by morselib.
bool mmosal_semb_wait(struct mmosal_semb *semb, uint32_t timeout_ms)
Wait for a counting semaphore.
struct mmosal_semb * mmosal_semb_create(const char *name)
Create a new binary semaphore.
bool mmosal_semb_give(struct mmosal_semb *semb)
Give a binary semaphore.
enum mmwlan_status mmwlan_dpp_stop(void)
Function to stop the DPP process.
mmwlan_dpp_pb_result
Enumeration of results for MMWLAN_DPP_EVT_PB_RESULT.
Definition: mmwlan.h:1070
enum mmwlan_status mmwlan_dpp_start(const struct mmwlan_dpp_args *args)
Function to start the Device Provisioning Protocol (DPP) process.
@ MMWLAN_DPP_PB_RESULT_SUCCESS
DPP push button process was successful.
Definition: mmwlan.h:1072
@ MMWLAN_DPP_PB_RESULT_SESSION_OVERLAP
A session overlap occurred during the DPP push button process.
Definition: mmwlan.h:1076
@ MMWLAN_DPP_PB_RESULT_ERROR
An error occurred during the DPP push button process.
Definition: mmwlan.h:1074
@ MMWLAN_DPP_EVT_PB_RESULT
DPP push button result.
Definition: mmwlan.h:1065
mmwlan_status
Enumeration of status return codes.
Definition: mmwlan.h:50
#define MMWLAN_SSID_MAXLEN
Maximum allowable length of an SSID.
Definition: mmwlan.h:79
@ MMWLAN_SUCCESS
The operation was successful.
Definition: mmwlan.h:52
Morse Micro application helper routines for initializing/de-initializing the Wireless LAN interface a...
void app_wlan_init(void)
Initializes the WLAN interface (and dependencies) using settings specified in the config store.
void app_wlan_start(void)
Starts the WLAN interface and connects to Wi-Fi using settings specified in the config store.
Structure to hold the arguments used for the DPP process.
Definition: mmwlan.h:1114
void(* dpp_event_cb)(const struct mmwlan_dpp_cb_args *dpp_event, void *arg)
DPP event callback prototype.
Definition: mmwlan.h:1116
Structure passed back when a DPP event occurs.
Definition: mmwlan.h:1086
union mmwlan_dpp_cb_args::@2 args
Union of arguments for DPP events.
const uint8_t * ssid
SSID of the AP to connect to.
Definition: mmwlan.h:1098
uint16_t ssid_len
Length of the SSID.
Definition: mmwlan.h:1100
const char * passphrase
Passphrase, NULL terminated.
Definition: mmwlan.h:1102
enum mmwlan_dpp_pb_result result
Result of DPP push button.
Definition: mmwlan.h:1096
struct mmwlan_dpp_cb_args::@2::@3 pb_result
Argument for MMWLAN_DPP_EVT_PB_RESULT event.
enum mmwlan_dpp_event event
The DPP event that has occurred.
Definition: mmwlan.h:1088