|
| 1 | +/** |
| 2 | + * WiiSX - fileBrowser-SMB.c |
| 3 | + * Copyright (C) 2010 emu_kidid |
| 4 | + * |
| 5 | + * fileBrowser module for Samba based shares |
| 6 | + * |
| 7 | + * WiiSX homepage: http://www.emulatemii.com |
| 8 | + * email address: emukidid@gmail.com |
| 9 | + * |
| 10 | + * |
| 11 | + * This program is free software; you can redistribute it and/ |
| 12 | + * or modify it under the terms of the GNU General Public Li- |
| 13 | + * cence as published by the Free Software Foundation; either |
| 14 | + * version 2 of the Licence, or any later version. |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be use- |
| 17 | + * ful, but WITHOUT ANY WARRANTY; without even the implied war- |
| 18 | + * ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 19 | + * See the GNU General Public Licence for more details. |
| 20 | + * |
| 21 | +**/ |
| 22 | + |
| 23 | +#ifdef HW_RVL |
| 24 | + |
| 25 | +#include <string.h> |
| 26 | +#include <unistd.h> |
| 27 | +#include <malloc.h> |
| 28 | +#include <network.h> |
| 29 | +#include <ogcsys.h> |
| 30 | +#include <smb.h> |
| 31 | +#include "fileBrowser.h" |
| 32 | +#include "fileBrowser-libfat.h" |
| 33 | +#include "fileBrowser-SMB.h" |
| 34 | + |
| 35 | +/* SMB Globals */ |
| 36 | +int net_initialized = 0; |
| 37 | +int smb_initialized = 0; |
| 38 | +// net init thread |
| 39 | +static lwp_t initnetthread = LWP_THREAD_NULL; |
| 40 | +static int netInitHalted = 0; |
| 41 | +static int netInitPending = 0; |
| 42 | + |
| 43 | +extern char smbUserName[]; |
| 44 | +extern char smbPassWord[]; |
| 45 | +extern char smbShareName[]; |
| 46 | +extern char smbIpAddr[]; |
| 47 | + |
| 48 | +fileBrowser_file topLevel_SMB = |
| 49 | + { "smb:/", // file name |
| 50 | + 0ULL, // discoffset (u64) |
| 51 | + 0, // offset |
| 52 | + 0, // size |
| 53 | + FILE_BROWSER_ATTR_DIR |
| 54 | + }; |
| 55 | + |
| 56 | +void resume_netinit_thread() { |
| 57 | + if(initnetthread != LWP_THREAD_NULL) { |
| 58 | + netInitHalted = 0; |
| 59 | + LWP_ResumeThread(initnetthread); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +void pause_netinit_thread() { |
| 64 | + if(initnetthread != LWP_THREAD_NULL) { |
| 65 | + netInitHalted = 1; |
| 66 | + |
| 67 | + if(netInitPending) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + // until it's completed for this iteration. |
| 72 | + while(!LWP_ThreadIsSuspended(initnetthread)) { |
| 73 | + usleep(100); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +// Init the GC/Wii net interface (wifi/bba/etc) |
| 80 | +static void* init_network(void *args) { |
| 81 | + |
| 82 | + char ip[16]; |
| 83 | + int res = 0, netsleep = 1*1000*1000; |
| 84 | + |
| 85 | + while(netsleep > 0) { |
| 86 | + if(netInitHalted) { |
| 87 | + LWP_SuspendThread(initnetthread); |
| 88 | + } |
| 89 | + usleep(100); |
| 90 | + netsleep -= 100; |
| 91 | + } |
| 92 | + |
| 93 | + while(1) { |
| 94 | + |
| 95 | + if(!net_initialized) { |
| 96 | + netInitPending = 1; |
| 97 | + res = if_config(ip, NULL, NULL, true); |
| 98 | + if(res >= 0) { |
| 99 | + net_initialized = 1; |
| 100 | + } |
| 101 | + else { |
| 102 | + net_initialized = 0; |
| 103 | + } |
| 104 | + netInitPending = 0; |
| 105 | + } |
| 106 | + |
| 107 | + netsleep = 1000*1000; // 1 sec |
| 108 | + while(netsleep > 0) { |
| 109 | + if(netInitHalted) { |
| 110 | + LWP_SuspendThread(initnetthread); |
| 111 | + } |
| 112 | + usleep(100); |
| 113 | + netsleep -= 100; |
| 114 | + } |
| 115 | + } |
| 116 | + return NULL; |
| 117 | +} |
| 118 | + |
| 119 | +void init_network_thread() { |
| 120 | + LWP_CreateThread (&initnetthread, init_network, NULL, NULL, 0, 40); |
| 121 | +} |
| 122 | + |
| 123 | +// Connect to the share specified in settings.cfg |
| 124 | +void init_samba() { |
| 125 | + |
| 126 | + int res = 0; |
| 127 | + |
| 128 | + if(smb_initialized) { |
| 129 | + return; |
| 130 | + } |
| 131 | + res = smbInit(&smbUserName[0], &smbPassWord[0], &smbShareName[0], &smbIpAddr[0]); |
| 132 | + if(res) { |
| 133 | + smb_initialized = 1; |
| 134 | + } |
| 135 | + else { |
| 136 | + smb_initialized = 0; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | + |
| 141 | +int fileBrowser_SMB_readDir(fileBrowser_file* ffile, fileBrowser_file** dir){ |
| 142 | + |
| 143 | + // We need at least a share name and ip addr in the settings filled out |
| 144 | + if(!strlen(&smbShareName[0]) || !strlen(&smbIpAddr[0])) { |
| 145 | + return SMB_SMBCFGERR; |
| 146 | + } |
| 147 | + |
| 148 | + if(!net_initialized) { //Init if we have to |
| 149 | + return SMB_NETINITERR; |
| 150 | + } |
| 151 | + |
| 152 | + if(!smb_initialized) { //Connect to the share |
| 153 | + init_samba(); |
| 154 | + if(!smb_initialized) { |
| 155 | + return SMB_SMBERR; //fail |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + // Call the corresponding FAT function |
| 160 | + return fileBrowser_libfat_readDir(ffile, dir); |
| 161 | +} |
| 162 | + |
| 163 | +int fileBrowser_SMB_seekFile(fileBrowser_file* file, unsigned int where, unsigned int type){ |
| 164 | + return fileBrowser_libfat_seekFile(file,where,type); |
| 165 | +} |
| 166 | + |
| 167 | +int fileBrowser_SMB_readFile(fileBrowser_file* file, void* buffer, unsigned int length){ |
| 168 | + return fileBrowser_libfatROM_readFile(file,buffer,length); |
| 169 | +} |
| 170 | + |
| 171 | +int fileBrowser_SMB_init(fileBrowser_file* file){ |
| 172 | + return 0; |
| 173 | +} |
| 174 | + |
| 175 | +int fileBrowser_SMB_deinit(fileBrowser_file* file) { |
| 176 | + /*if(smb_initialized) { |
| 177 | + smbClose("smb"); |
| 178 | + smb_initialized = 0; |
| 179 | + }*/ |
| 180 | + return fileBrowser_libfatROM_deinit(file); |
| 181 | +} |
| 182 | + |
| 183 | +#endif |
0 commit comments