Morse Micro IoT SDK  2.10.4
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
33{
34 switch (result)
35 {
37 return "Success";
39 return "Error";
41 return "Session Overlap";
42 default:
43 return "Unknown Result";
44 }
45}
46
53static void dpp_event_handler(const struct mmwlan_dpp_cb_args *dpp_event, void *arg)
54{
55 struct mmosal_semb *semb = (struct mmosal_semb *)arg;
56 if (dpp_event->event != MMWLAN_DPP_EVT_PB_RESULT)
57 {
58 mmosal_printf("Unsupported event %lu\n", dpp_event->event);
59 return;
60 }
61
62 mmosal_printf("DPP Result: %s\n",
64
66 {
67 return;
68 }
69
70 if ((dpp_event->args.pb_result.ssid == NULL) ||
71 (dpp_event->args.pb_result.passphrase == NULL) ||
72 (dpp_event->args.pb_result.ssid_len > MMWLAN_SSID_MAXLEN - 1))
73 {
74 mmosal_printf("Invalid/incomplete credentials provided\n");
75 return;
76 }
77
78 mmosal_printf("SSID %*s, PWD %s\n",
79 dpp_event->args.pb_result.ssid_len,
80 dpp_event->args.pb_result.ssid,
81 dpp_event->args.pb_result.passphrase);
82
83 char ssid[MMWLAN_SSID_MAXLEN];
84 memcpy(ssid, dpp_event->args.pb_result.ssid, dpp_event->args.pb_result.ssid_len);
85 ssid[dpp_event->args.pb_result.ssid_len] = '\0';
86 mmconfig_write_string("wlan.ssid", ssid);
87 mmconfig_write_string("wlan.password", dpp_event->args.pb_result.passphrase);
88
89 (void)mmosal_semb_give(semb);
90}
91
96void app_init(void)
97{
98 printf("\n\nSTA DPP Connect Example (Built " __DATE__ " " __TIME__ ")\n\n");
99
100 struct mmosal_semb *semb = mmosal_semb_create("dpp");
101 MMOSAL_ASSERT(semb);
102
104
105 struct mmwlan_dpp_args dpp_args = { .dpp_event_cb = dpp_event_handler,
106 .dpp_event_cb_arg = semb };
107
108 mmosal_printf("DPP Start\n");
109 enum mmwlan_status status = mmwlan_dpp_start(&dpp_args);
110 MMOSAL_ASSERT(status == MMWLAN_SUCCESS);
111
112 bool ok = mmosal_semb_wait(semb, 200 * 1000);
113 MMOSAL_ASSERT(ok);
114
116
118}
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:32
static void dpp_event_handler(const struct mmwlan_dpp_cb_args *dpp_event, void *arg)
Function to handle dpp events.
Definition: dpp.c:53
void app_init(void)
Main entry point to the application.
Definition: dpp.c:96
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:934
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:2141
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:2143
@ MMWLAN_DPP_PB_RESULT_SESSION_OVERLAP
A session overlap occurred during the DPP push button process.
Definition: mmwlan.h:2147
@ MMWLAN_DPP_PB_RESULT_ERROR
An error occurred during the DPP push button process.
Definition: mmwlan.h:2145
@ MMWLAN_DPP_EVT_PB_RESULT
DPP push button result.
Definition: mmwlan.h:2136
mmwlan_status
Enumeration of status return codes.
Definition: mmwlan.h:51
#define MMWLAN_SSID_MAXLEN
Maximum allowable length of an SSID.
Definition: mmwlan.h:86
@ MMWLAN_SUCCESS
The operation was successful.
Definition: mmwlan.h:53
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:2186
void(* dpp_event_cb)(const struct mmwlan_dpp_cb_args *dpp_event, void *arg)
DPP event callback prototype.
Definition: mmwlan.h:2188
Structure passed back when a DPP event occurs.
Definition: mmwlan.h:2157
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:2170
uint16_t ssid_len
Length of the SSID.
Definition: mmwlan.h:2172
const char * passphrase
Passphrase, NULL terminated.
Definition: mmwlan.h:2174
enum mmwlan_dpp_pb_result result
Result of DPP push button.
Definition: mmwlan.h:2168
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:2159