xref: /btstack/src/classic/obex_srm_client.c (revision e3bb42c259e0e2b54fb29afff9b1ba86c21b3610)
15fe90018SMatthias Ringwald /*
25fe90018SMatthias Ringwald  * Copyright (C) 2024 BlueKitchen GmbH
35fe90018SMatthias Ringwald  *
45fe90018SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
55fe90018SMatthias Ringwald  * modification, are permitted provided that the following conditions
65fe90018SMatthias Ringwald  * are met:
75fe90018SMatthias Ringwald  *
85fe90018SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
95fe90018SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
105fe90018SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
115fe90018SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
125fe90018SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
135fe90018SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
145fe90018SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
155fe90018SMatthias Ringwald  *    from this software without specific prior written permission.
165fe90018SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
175fe90018SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
185fe90018SMatthias Ringwald  *    monetary gain.
195fe90018SMatthias Ringwald  *
205fe90018SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
215fe90018SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
225fe90018SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
235fe90018SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
245fe90018SMatthias Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
255fe90018SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
265fe90018SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
275fe90018SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
285fe90018SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
295fe90018SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
305fe90018SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315fe90018SMatthias Ringwald  * SUCH DAMAGE.
325fe90018SMatthias Ringwald  *
335fe90018SMatthias Ringwald  * Please inquire about commercial licensing options at
345fe90018SMatthias Ringwald  * [email protected]
355fe90018SMatthias Ringwald  *
365fe90018SMatthias Ringwald  */
375fe90018SMatthias Ringwald 
38*e3bb42c2SMatthias Ringwald #define BTSTACK_FILE__ "obex_srm_client.c"
395fe90018SMatthias Ringwald 
405fe90018SMatthias Ringwald #include "btstack_debug.h"
415fe90018SMatthias Ringwald 
425fe90018SMatthias Ringwald #include "classic/goep_client.h"
435fe90018SMatthias Ringwald #include "classic/obex.h"
445fe90018SMatthias Ringwald #include "classic/obex_srm_client.h"
455fe90018SMatthias Ringwald 
obex_srm_client_init(obex_srm_client_t * obex_srm)465fe90018SMatthias Ringwald void obex_srm_client_init(obex_srm_client_t * obex_srm){
475fe90018SMatthias Ringwald     obex_srm->srm_state = OBEX_SRM_CLIENT_STATE_DISABLED;
48*e3bb42c2SMatthias Ringwald     obex_srm->srmp_waiting = false;
4930f40b4aSMatthias Ringwald     obex_srm_client_reset_fields(obex_srm);
5030f40b4aSMatthias Ringwald }
5130f40b4aSMatthias Ringwald 
obex_srm_client_set_waiting(obex_srm_client_t * obex_srm,bool waiting)52*e3bb42c2SMatthias Ringwald void obex_srm_client_set_waiting(obex_srm_client_t * obex_srm, bool waiting){
53*e3bb42c2SMatthias Ringwald     log_info("Set SRMP Waiting: %u", (int) waiting);
54*e3bb42c2SMatthias Ringwald     obex_srm->srmp_waiting = waiting;
55*e3bb42c2SMatthias Ringwald }
56*e3bb42c2SMatthias Ringwald 
obex_srm_client_reset_fields(obex_srm_client_t * obex_srm)5730f40b4aSMatthias Ringwald void obex_srm_client_reset_fields(obex_srm_client_t * obex_srm){
585fe90018SMatthias Ringwald     obex_srm->srm_value = OBEX_SRM_DISABLE;
595fe90018SMatthias Ringwald     obex_srm->srmp_value = OBEX_SRMP_NEXT;
605fe90018SMatthias Ringwald }
615fe90018SMatthias Ringwald 
obex_srm_client_handle_headers(obex_srm_client_t * obex_srm)625fe90018SMatthias Ringwald void obex_srm_client_handle_headers(obex_srm_client_t *obex_srm) {
635fe90018SMatthias Ringwald     // Update SRM state based on SRM headers
645fe90018SMatthias Ringwald     switch (obex_srm->srm_state){
655fe90018SMatthias Ringwald         case OBEX_SRM_CLIENT_STATE_W4_CONFIRM:
665fe90018SMatthias Ringwald             switch (obex_srm->srm_value){
675fe90018SMatthias Ringwald                 case OBEX_SRM_ENABLE:
685fe90018SMatthias Ringwald                     switch (obex_srm->srmp_value){
695fe90018SMatthias Ringwald                         case OBEX_SRMP_WAIT:
705fe90018SMatthias Ringwald                             obex_srm->srm_state = OBEX_SRM_CLIENT_STATE_ENABLED_BUT_WAITING;
715fe90018SMatthias Ringwald                             break;
725fe90018SMatthias Ringwald                         default:
735fe90018SMatthias Ringwald                             obex_srm->srm_state = OBEX_SRM_CLIENT_STATE_ENABLED;
745fe90018SMatthias Ringwald                             break;
755fe90018SMatthias Ringwald                     }
765fe90018SMatthias Ringwald                     break;
775fe90018SMatthias Ringwald                 default:
785fe90018SMatthias Ringwald                     obex_srm->srm_state = OBEX_SRM_CLIENT_STATE_DISABLED;
795fe90018SMatthias Ringwald                     break;
805fe90018SMatthias Ringwald             }
815fe90018SMatthias Ringwald             break;
825fe90018SMatthias Ringwald         case OBEX_SRM_CLIENT_STATE_ENABLED_BUT_WAITING:
835fe90018SMatthias Ringwald             switch (obex_srm->srmp_value){
845fe90018SMatthias Ringwald                 case OBEX_SRMP_WAIT:
855fe90018SMatthias Ringwald                     obex_srm->srm_state = OBEX_SRM_CLIENT_STATE_ENABLED_BUT_WAITING;
865fe90018SMatthias Ringwald                     break;
875fe90018SMatthias Ringwald                 default:
885fe90018SMatthias Ringwald                     obex_srm->srm_state = OBEX_SRM_CLIENT_STATE_ENABLED;
895fe90018SMatthias Ringwald                     break;
905fe90018SMatthias Ringwald             }
915fe90018SMatthias Ringwald             break;
925fe90018SMatthias Ringwald         default:
935fe90018SMatthias Ringwald             break;
945fe90018SMatthias Ringwald     }
95*e3bb42c2SMatthias Ringwald     log_info("Handle SRM %u, SRMP %u -> new SRM state %u", obex_srm->srm_value, obex_srm->srmp_value,obex_srm->srm_state);
965fe90018SMatthias Ringwald }
975fe90018SMatthias Ringwald 
obex_srm_client_prepare_header(obex_srm_client_t * obex_srm,uint16_t goep_cid)985fe90018SMatthias Ringwald void obex_srm_client_prepare_header(obex_srm_client_t *obex_srm, uint16_t goep_cid){
995fe90018SMatthias Ringwald     if (goep_client_version_20_or_higher(goep_cid)){
100*e3bb42c2SMatthias Ringwald         // add SRM Enable Header if not enabled yet
101*e3bb42c2SMatthias Ringwald         if (obex_srm->srm_state == OBEX_SRM_CLIENT_STATE_DISABLED){
1025fe90018SMatthias Ringwald             obex_srm->srm_state = OBEX_SRM_CLIENT_STATE_W4_CONFIRM;
103*e3bb42c2SMatthias Ringwald             log_info("Prepare: add SRM Enable");
104*e3bb42c2SMatthias Ringwald             goep_client_header_add_srm_enable(goep_cid);
105*e3bb42c2SMatthias Ringwald         }
106*e3bb42c2SMatthias Ringwald         // add SRMP Waiting header if waiting is active
107*e3bb42c2SMatthias Ringwald         if (obex_srm->srmp_waiting){
108*e3bb42c2SMatthias Ringwald             log_info("Prepare: add SRMP Waiting");
109*e3bb42c2SMatthias Ringwald             goep_client_header_add_srmp_waiting(goep_cid);
110*e3bb42c2SMatthias Ringwald         }
1115fe90018SMatthias Ringwald     }
1125fe90018SMatthias Ringwald }
11330f40b4aSMatthias Ringwald 
obex_srm_client_is_srm_active(obex_srm_client_t * obex_srm)11430f40b4aSMatthias Ringwald bool obex_srm_client_is_srm_active(obex_srm_client_t *obex_srm){
115*e3bb42c2SMatthias Ringwald     return (obex_srm->srm_state == OBEX_SRM_CLIENT_STATE_ENABLED) && (obex_srm->srmp_waiting == false);
11630f40b4aSMatthias Ringwald }
117