summaryrefslogtreecommitdiff
path: root/bluetooth/bthecho/bthsrv/inst/main.cpp
blob: ab555b2b605d072d00e8c48e7bf1a123a008b2c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*++

Copyright (c) Microsoft Corporation.  All rights reserved.

    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
    KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
    PURPOSE.

Module Name:

    Main.cpp

Abstract:

    Utility for installing and uninstalling WdfBthSampleSrv echo bth server.

    bthsrvinst /i (installs bth service) 
    bthsrvinst /u (uninstalls bth service)

Environment:

    user mode only

--*/

#include <windows.h>
#include <bthsdpdef.h>
#include <bthdef.h>
#include <BlueToothApis.h>
#include <stdio.h>
#include <strsafe.h>
#include <initguid.h>
#include "public.h"

#define SWITCH_ARG_LEN 2

DWORD
ReadSwitch(
    _In_ LPCWSTR SwitchArg,
    _Out_ WCHAR * Switch
    );

BOOL
AdjustProcessPrivileges(
    );

DWORD
SetBthServiceInfo(
    BOOLEAN bEnabled
    );

void
__cdecl
wmain(
    _In_ UINT nArgs,
    _In_reads_(nArgs) LPCWSTR Args[]
    )
{
    if (nArgs < 2)
    {
        goto usage;
    }

    WCHAR Switch;

    if (ERROR_SUCCESS != ReadSwitch(Args[1], &Switch))
    {
        goto exit;
    }

    if (FALSE == AdjustProcessPrivileges())
    {
        goto exit;
    }

    switch(Switch)
    {
        case L'i':
            SetBthServiceInfo(TRUE);
            break;
        case L'u':
            SetBthServiceInfo(FALSE);
            break;
        default:
            goto usage;
    }    
    
exit:
    return;
    
usage:
    printf("Usage: bthsrvinst [/i | /u]\n");
    goto exit;
}

DWORD SetBthServiceInfo(
    BOOLEAN bEnabled
    )
{
    DWORD err = ERROR_SUCCESS;
    BLUETOOTH_LOCAL_SERVICE_INFO SvcInfo = {0};
    SvcInfo.Enabled = bEnabled;

    if (FAILED(StringCbCopyW(SvcInfo.szName, sizeof(SvcInfo.szName), BthEchoSampleSvcName)))
    {
        printf("Copying svc name failed\n");
        goto exit;
    }

    if (ERROR_SUCCESS != (err = BluetoothSetLocalServiceInfo(
        NULL, //callee would select the first found radio
        &BTHECHOSAMPLE_SVC_GUID,
        0,
        &SvcInfo
        )))
    {
        printf("BluetoothSetLocalServiceInfo failed, err = %d\n", err);
        goto exit;        
    }
exit:
    return err;    
}

DWORD
ReadSwitch(
    _In_ LPCWSTR SwitchArg,
    _Out_ WCHAR * Switch
    )
{
    DWORD retval = ERROR_INVALID_PARAMETER;

    if (NULL == Switch) {
        printf("Switch should not be null");
        goto exit;
    }
    *Switch = 0;
    
    size_t len = 0;
    if (FAILED(StringCchLength(SwitchArg, SWITCH_ARG_LEN+1, &len)) ||
        len != SWITCH_ARG_LEN)
    {
        printf("Invalid arg %S, expected arg length:0x%x, actual length: 0%Ix\n", SwitchArg, SWITCH_ARG_LEN, len);
        goto exit;
    }

    if (SwitchArg[0] != L'/' && SwitchArg[0] != L'-')
    {
        printf("Invalid first character in arg %S\n", SwitchArg);
        goto exit;        
    }

    retval = ERROR_SUCCESS;
    *Switch = SwitchArg[1];
exit:
    return retval;
}

BOOL
AdjustProcessPrivileges(
    )
{
    HANDLE procToken;
    LUID luid;
    TOKEN_PRIVILEGES tp;
    BOOL bRetVal;
    DWORD err;

    bRetVal = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &procToken);
    if (!bRetVal)
    {
        err = GetLastError();
        printf("OpenProcessToken failed, err = %d\n", err);
        goto exit;
    }
        
    bRetVal = LookupPrivilegeValue(NULL, SE_LOAD_DRIVER_NAME, &luid);
    if (!bRetVal)
    {
        err = GetLastError();
        printf("LookupPrivilegeValue failed, err = %d\n", err);
        goto exit1;
    }

    tp.PrivilegeCount = 1;
    tp.Privileges[0].Luid = luid;
    tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    //
    // AdjustTokenPrivileges can succeed even when privileges are not adjusted.
    // In such case GetLastError returns ERROR_NOT_ALL_ASSIGNED.
    //
    // Hence we check for GetLastError in both success and failure case.
    //

    (void) AdjustTokenPrivileges(procToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES) NULL, (PDWORD)NULL);
    err = GetLastError();

    if (err != ERROR_SUCCESS)
    {
        bRetVal = FALSE;
        printf("AdjustTokenPrivileges failed, err = %d\n", err);
        goto exit1;
    }

exit1:
    CloseHandle(procToken);
exit:
    return bRetVal;
}